Maniging relations in rails -
i working on application , have create model product
has many pictures
, main picture
. want model situation through relations , not using additional boolean field can tell if 1 picture main picture or not. think solution use has_one
, has_many
relations in product
both link picture
don't know how this.
you're going need way distinguish "main" picture other pictures. can done in number of ways:
a separate database table, say, main_pictures
class product has_many :pictures has_one :main_picture end
or other attribute on picture
. can boolean or other field. in example, below, we'll use boolean attribute named primary
on pictures
table.
class product has_many :pictures has_one :main_picture, -> { where(primary: true) } end
Comments
Post a Comment