Let's say you have two models Venue
and Photo
. Each Venue
can have many Photos
, but it can only have one FeaturedPhoto
. Each Photo
can be belong to many Venues
and can be the FeaturedPhoto
of more than one Venue too.
I already have the general has_and_belongs_to_many relationship set up and working through a join table with 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 the FeaturedPhoto, it seems like I would need to add a column called featured_photo_id to the Venue model and then set up a has_many, belongs_to association between the two. But I'm not sure where to add the foreign_key info. Is this 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 do I have to add foreign_key info to both models?