Hi all,
is it possible to use scope in model relationships?
example
class Post < ActiveRecord::Base
scope :published, where(:published, true)
end
class User < ActiveRecord::Base
has_many :posts
has_many :published_posts, class_name: ‘Post’ … here I would like
to
use the same conditions of scope published without rewrite them
I know that I can write a method in User class instead of the has_many
relationship, something like
def published_post
return self.posts.published
end
but in this way I can’t use the include: in the to_json method
@user.to_json(include: :published_posts)
and I can’t use method: instead beacuse I have to use also the only:
attribute to filter the attributes in the json output
Thanks to all