gpt4 book ai didi

ruby-on-rails - current_user 的 Rails 关联

转载 作者:行者123 更新时间:2023-12-01 06:41:57 24 4
gpt4 key购买 nike

我前段时间读到这篇文章:http://pivotallabs.com/users/nick/blog/articles/275-advanced-proxy-usage-part-i这是在谈论 AR 代理等。

作者指出我现在的问题是什么。

本例将对其进行说明。

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

和这里
class Image
belongs_to :user
belongs_to :gallery
end

注意:我使用的是 PostgreSQL

所以当你访问 我的 所有带有画廊的图像: Gallery.first.includes(:my_images)
如果我想返回所有图像: Gallery.first.includes(:images)
那么现在如何将当前用户传递给 has_many中的条件?

编辑
User
has_many :images
has_many :galleries, through: :images

最佳答案

如果我理解得很好,你想得到类似的东西:

current_user.galleries.first.images

如果是这样,您只需要通过第三个模型(可能名为 UserGallery)在 User 和 Gallery 之间创建一个 has_many 关联。

更多信息有很多通过:

http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

关于ruby-on-rails - current_user 的 Rails 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9676311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com