ruby - Rails find_by_name on another table -


i have genders , based on gender name create category , subcategories.

m = gender.create(:gender => 'masculine')  c = category.find_by_name("t-shirt", gender: m ) c.subcategories.create(:name => "necklace" ) 

and on.

you can try this.

m = gender.create(:gender => 'masculine')  c = category.where(name: "t-shirt", gender: m.gender ).first  c.subcategories.create(name: "necklace" ) 

please note above code raise exception if no category found given condition, avoid can use following.

m = gender.create(:gender => 'masculine')  c = category.where(name: "t-shirt", gender: m.gender).try(:first)  unless c.blank?   c.subcategories.create(name: "necklace" ) end 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -