很抱歉,如果我的标题有点不具体,不知道如何在没有示例代码的情况下表达这个问题。
下面是:
型号:
class Profile < ActiveRecord::Base
has_one :gallery
end
class Gallery < ActiveRecord::Base
has_many :images
scope :processed_images, -> { joins(:images).on('image.image_processing = false')
end
class Image < ActiveRecord::Base
mount_uploader :image
store_in_background :image
end我只想通过关联获得处理过的图像,比如:
profile.gallery.processed_images编辑:或者也许
profile.gallery.images.processed或者在sql中:
select * from images where image.gallery_id = ? and image.image_processing = false我可以通过profile.gallery协会吗?或者我必须做一些类似的事情
Image.where('gallery_id = ? and image_processing = false', gallery_id)谢谢!
发布于 2014-08-07 10:35:22
解决方案1:
将范围定义更改为
has_many :processed_images, -> { where(image_processing: false) }, class_name: 'Image'请记住,scope用于筛选当前类,因此在图片库模型中定义作用域将意味着要筛选图库表。
然后打电话给profile.gallery.processed_images
解决方案2:
将图片库模型中的范围移动到图像模型
scope :processed, -> { where(image_processing: false) }然后打电话给profile.gallery.images.processed
https://stackoverflow.com/questions/25180200
复制相似问题