@Jon Leighton
Can that scope be removed later? Can I retrieve all comments, including hidden
ones?
--
Dheeraj Kumar
On Monday 3 June 2013 at 5:33 PM, Jon Leighton wrote:
> In Rails 4 you should be able to do:
>
> class Post
> has_many :comments, -> { visible }
> end
>
> On 21/05/13 23:41, Caleb Thompson wrote:
> > I'm considering implementing a feature by which a collection
> > association might limit the results.
> >
> > By default, a collection association returns all values where the
> > foreign key on the `belongs_to` or `has_and_belongs_to` model matches
> > the parent object's primary key. These results can be filtered using the
> > `conditions` option, but that requires that other models have knowledge
> > of the parent model's table structure.
> >
> > The feature I'm proposing is to add a `scope` option to collection
> > associations which takes a symbol representing a scope (or class mehtod)
> > defined on the associated model class.
> >
> > A basic example would look like this:
> >
> > class Comment
> > belongs_to :post
> >
> > def self.visible
> > where(deleted_at: nil)
> > end
> > end
> >
> > class Post
> > has_many :posts, scope: :visible
> > end
> >
> > In the above example, `post.comments` would return `Comment` instances
> > whose `post_id = post.id` and whose `deleted_at = NULL`, providing basic
> > soft-deletion functionality.
> >
> > While the same effect could be achieved with a declaration such as
> > `has_many :posts, conditions: ['deleted_at = ?', nil]`, that betrays
> > knowledge of the implementation of deletion on `Post` and would break if
> > the implementation were changed to a boolean value for deletion rather
> > than a timestamp field.
> >
> > If I were to work on adding this functionality into ActiveRecord, is it
> > something that core might entertain merging?
> >
> > Thank you,
> >
> > Caleb Thompson
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Ruby on Rails: Core" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to [email protected]
> > (mailto:[email protected]).
> > To post to this group, send email to [email protected]
> > (mailto:[email protected]).
> > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
> >
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> (mailto:[email protected]).
> To post to this group, send email to [email protected]
> (mailto:[email protected]).
> Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.