Rails error: add + include
I have the following models:
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
I am trying to get all images that are public and at the same time being downloaded by the people who own those images:
Image.find(:all, :conditions => ["images.person_id = ? AND preferences.image_privacy = ?", user.id, PRIVACY_PUBLIC],
:joins => [:person => :user_preference], :include => :person)
It looks like Rails doesn't like: include (I believe because: the person is mentioned in 2 models). This is the error I'm getting (which goes away when I drop the: include parameter):
"ActiveRecord :: StatementInvalid: Mysql :: Error: Not unique table / alias:" people "
I can work around this by writing out the actual JOIN command as a string and passing it to the: include parameter, but this is not Rails-y, so I was hoping there was a cleaner way to do this.
Any help would be much appreciated.
Thank!
source to share
This may be a problem with the Table Aliasing, rails doc has more details in the Smoothing tables
also, post SQL will be helpful here too.
source to share
You wrote: conditions => ["images.person_id", user.id] and you said you want to upload images and the people who own those images. But it looks like you are loading images that are owned by one person (not a group of people) because you are only specifying one user.id.
I would do it like this:
Person.find(user.id, :include => [:images, :preference], :conditions => ["preferences.image_privacy = ?", PRIVACY_PUBLIC])
It will upload the person and their images.
I may be misunderstanding your problem because what I think you want to do doesn't seem logical to me.
Instead of using conditions, you can try named_scope
source to share