我是Rails的初学者,
我们可以使用:source
命名范围吗?
我可以将它与 has_many 和其他关联一起使用
谢谢马克
不,你不能,因为你不需要。命名范围是其中定义的模型的一部分。
class Post
named_scope :published, :conditions => { :published => true }
end
但是,这并不妨碍您通过关联使用命名范围。
class Category
has_many :posts
end
category.posts # => all posts
category.posts.published # only published posts
如果可以在 find() 调用中使用它,通常可以将它与命名范围一起使用。find 的参数在文档(http://apidock.com/rails/ActiveRecord/Base/find/class)中逐项列出,但我不确定源是其中之一。据我所知,这是针对 has_many 关系之类的,而不是针对 find 的。
但是,命名范围可以应用于关系,所以也许这就是您想要的。