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
Post a Comment