views:

41

answers:

2

I am a beginner in Rails,

Can we use :source with named scope?

I am able to use it with has_many and other associations

Thanks Mark

A: 

If you can use it in a find() call, generally you can use it with a named scope. The parameters for find are itemized in the documentation (http://apidock.com/rails/ActiveRecord/Base/find/class) but I'm not sure that source is one of them. As far as I know, that's for a has_many relationship sort of thing, not for find.

However, named scopes can be applied to relationships, so perhaps that's what you're intending.

tadman
+1  A: 

No, you can't because you don't need. A named scope is part of the model where defined in.

class Post
  named_scope :published, :conditions => { :published => true }
end

However, this doesn't prevent you from using a named scope through an association.

class Category
  has_many :posts
end

category.posts # => all posts
category.posts.published # only published posts
Simone Carletti