ruby on rails - has_many belongs_to association with a foreign key -
let's have 2 models venue , photo. each venue can have many photos, can have one featuredphoto. each photo can belong many venues , can featuredphoto of more 1 venue too.
i have general has_and_belongs_to_many relationship set , working through join table these models:
class photo < activerecord::base mount_uploader :image, photouploader has_and_belongs_to_many :venues end class venue < activerecord::base has_and_belongs_to_many :photos end to add featuredphoto, seems need add column called featured_photo_id venue model , set has_many, belongs_to association between two. i'm not sure add foreign_key info. right?
class photo < activerecord::base mount_uploader :image, photouploader has_and_belongs_to_many :venues has_many :venues end class venue < activerecord::base has_and_belongs_to_many :photos belongs_to :photo, foreign_key: "featured_photo_id", class_name: "photo" end or have add foreign_key info both models?
this can achieved if add model intermediate table photosvenue , add boolean column is_featured in intermediate table.
i created demo app want. check github repo
hope helps :)
Comments
Post a Comment