我有以下模型:
class Person < ActiveRecord::Base
has_many :images
has_one :preference
end
class Image < ActiveRecord::Base
belongs_to :person
end
class Preference < ActiveRecord::Base
belongs_to :person
end
我正在尝试获取所有公开的图片,并同时急切加载拥有这些图片的人:
Image.find(:all, :conditions => ["images.person_id = ? AND preferences.image_privacy = ?", user.id, PRIVACY_PUBLIC],
:joins => [:person => :user_preference], :include => :person)
似乎Rails不喜欢:include(我认为是因为:person在2个模型中被引用)。这是我得到的错误(当我删除:include选项时,它就消失了):“ActiveRecord::StatementInvalid:Mysql :: Error:Not unique table / alias:'people'”。我可以通过编写实际的JOIN命令作为字符串并将其传递到:include选项中来解决此问题,但这不符合Rails-y的方式,因此我希望有更清晰的方法来解决这个问题。非常感谢您的帮助。谢谢!