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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Vb3ZXv0zyZIJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 31, '12 at 11:30a
activeJan 31, '12 at 11:30a
posts1
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Grentis: 1 post

People

Translate

site design / logo © 2021 Grokbase