activerecord - Rails - Efficiently checking child attributes exist -
probably basic question can't seem figure out.. have models clothes , colours.
what's efficient way of checking active-record if clothing has colour in or not, piece of clothing has multiple colours. boolean if possible.
since (presumably) many clothings can have same colour , clothing has many colours, you'd need join table , (simplest case) using habtm relationship.
as you're checking particular piece of clothing (i.e. have clothing model loaded), can efficiently model method.
you create method has_colour?
class clothing < applicationrecord has_and_belongs_to_many :colours def has_colour?(colour) colours.include?(colour) end end
Comments
Post a Comment