1

I have two separate has many through one relationships pointing to the same objects.

  • Users have many photos through photo_relationships
  • Users have many photos through votes

In my controller I'm trying to show all of the photos for the user through this code:

@user = User.find(params[:id])
@photos = @user.photos

However, the Inner Join is being controlled by whatever has_many relationships is mentioned last in the User model, in this case votes. Is there a way to specify what inner join is used such as:

@photos = @user.photos( joins: :photo_relationships)
Mitch Nick
  • 302
  • 2
  • 8

1 Answers1

0

Do something like this:

class User
  ...
  has_many :voted_photos,        class_name: 'Photo', through: :votes
  has_many :relationship_photos, class_name: 'Photo', through: :photo_relationships
end
tybro0103
  • 48,327
  • 33
  • 144
  • 170
  • Thanks much! I added a source: :photo onto the code as well, this allowed me to leave the other models alone. [This post](http://stackoverflow.com/questions/408872/rails-has-many-through-find-by-extra-attributes-in-join-model) also helped me once you got me going in the right direction. – Mitch Nick Mar 04 '13 at 20:06