Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails has_one :through. Building associated object

I have the following data model in my Rails 2.3 application

class PortraitSubject
  has_many    :portraits
  has_one     :primary_portrait, :through => :portraits, :source => :asset, :conditions => ['portraits.primary = ?', true]
  has_many    :supplementary_portraits, :through => :portraits, :source => :asset, :conditions => ['portraits.primary = ?', false]

  ...
end

class Portrait
  belongs_to :portrait_subject
  belongs_to :asset

  ...
end

I want to build the associated proxy models using Rails but trying to build primary_portrait fails with an exception. I.e.

# This works
subject = PortraitSubject.new
subject.supplementary_portraits.build
subject.save

# This doesn't
subject = PortraitSubject.new
subject.build_primary_portrait
# => NoMethodError: undefined method `build_primary_portrait' for #<PortraitSubject:0x007ff16fe38948>

I'm not sure what I'm doing wrong. Looking through the Rails guides it looks like this should be possible with a has_one relationship. Any help would be greatly appreciated.

like image 540
Bart Jedrocha Avatar asked Apr 08 '13 18:04

Bart Jedrocha


1 Answers

You are going to go crazy with those naming conventions. A PrimaryPortrait and a SecondaryPortrait ought to be special cases of Portrait not the assets that belong to a Portrait. It's already breaking your design that you can't build one.

Try this:

class PortraitSubject
  has_many    :portraits
  has_one     :primary_portrait, :conditions => {:primary => true}
  has_many    :supplementary_portraits, :conditions => {:primary => false}

  has_many    :portrait_assests, :through => :portraits
  has_one     :primary_portrait_asset, :through => :primary_portrait
  has_many    :supplementary_portrait_assets, :through => :supplementary_portraits

end

then, if you need to build a primary_portait_asset write an instance method

def build_primary_portrait_asset
  primary_portrait || build_primary_portrait
  primary_portrait.asset || primary_portrait.build_asset
end
like image 185
Ray Baxter Avatar answered Oct 31 '22 04:10

Ray Baxter