Rails has_one relationship where foreign key is on owning model? -


i have photo , photoset model:

a photoset should have many photos , 1 primary_photo. photo should belong many photosets , can primary_photo of many photosets.

however stumped how setup primary_photo relation. envisioning adding primary_photo_id photosets can't figure out how define has_one column defined on owning side (photoset).

i tried implement by:

  • adding photo_photosets join table.
  • adding primary_photo_id column photosets.

i setup relations in models:

class photo < activerecord::base   has_and_belongs_to_many :photosets   validates_uniqueness_of :flickr_uid end  class photoset < activerecord::base   belongs_to :user   validates_uniqueness_of :flickr_uid   has_and_belongs_to_many :photos   has_one :primary_photo, class_name: 'photo' # !??? end 

    class photo < activerecord::base       has_and_belongs_to_many :photosets       validates_uniqueness_of :flickr_uid     end      class photoset < activerecord::base       belongs_to :user       validates_uniqueness_of :flickr_uid       has_and_belongs_to_many :photos       belongs_to :primary_photo, class_name: 'photo'     end 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -