Rails associations for current_user
I read this article some time ago: http://pivotallabs.com/users/nick/blog/articles/275-advanced-proxy-usage-part-i , which talks about AR proxies, etc.
The author pointed out what the problem is now.
This example will describe it.
class Gallery
has_many :images, :class_name => 'Image'
has_many :my_images, :class_name => 'Image' #, :conditions => "images.user_id == current_user.id" # FIX THIS
end
and here
class Image
belongs_to :user
belongs_to :gallery
end
Note: I am using PostgreSQL
So when you access my all gallery images:Gallery.first.includes(:my_images)
If I want to return all images: Gallery.first.includes(:images)
So how do you pass the current user to the conditions in has_many
?
Edit
User
has_many :images
has_many :galleries, through: :images
source to share
If I understand well, you want to end up with something like:
current_user.galleries.first.images
, has_many , , UserGallery.
:
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association