开发者

Eager loading polymorphic associations with different has_one associations in Rails 2

开发者 https://www.devze.com 2023-01-04 06:19 出处:网络
ActiveRecord gives you an interesting error when you try to eager-load a non-existent association. It looks something like this:

ActiveRecord gives you an interesting error when you try to eager-load a non-existent association. It looks something like this:

ActiveRecord::ConfigurationError: Association named 'secondary_complaint' was not found; perhaps you misspelled it?

Now why the hell would anybody want to preload a non-existent association? Check this out.

class Bitchy < ActiveRecord::Base
  has_one :primary_complaint, :as => :whiny_bitch, :class_name => 'Complaint', :conditions => {:complaint_type => 'primary'}
  has_one :secondary_complaint, :as => :whiny_bitch, :class_name => 'Complaint', :conditions => {:complaint_type => 'secondary'}

  has_one :life, :a开发者_如何学Gos => :humanoid
end


class Whiny < ActiveRecord::Base
  has_one :primary_complaint, :as => :whiny_bitch, :class_name => 'Complaint', :conditions => {:complaint_type => 'primary'}

  has_one :life, :as => :humanoid
end

class Complaint < ActiveRecord::Base
  belongs_to :whiny_bitch, :polymorphic => true
end

class Life < ActiveRecord::Base
  belongs_to :humanoid, :polymorphic => true
end

# And here's the eager-loading part:
Life.all(:include => {:humanoid => [:primary_complaint, :secondary_complaint]})

The above code has interesting peculiarity. If you only have Bitchy as your humanoids - it will actually work. However, as soon as a single Whiny appears — you're in trouble. ActiveRecord starts whining the error I wrote above - Association named 'secondary_complaint' was not found. You see why, right? Because not every humanoid has a secondary_complaint.

Is there a way to make ActiveRecord stop bitching and whining when I'm trying to eager load polymorphic associations which may or may not have certain has_one associations attached to them?


I know this is probably just for the sake of your example, but you could change it to has_many :complaints so they both have the same association, and then pull out the primary or secondary types from that.

0

精彩评论

暂无评论...
验证码 换一张
取 消