> except that won't work, because either the association is eagerly
included or it isn't.
self-correction: You could have an association that defined include and
another that didn't to the same model- it isn't eager loading just because
you defined another association that uses the include option. But the end
result is the same- you likely end up defining the include in the
controller in one action method and not the other.
On Monday, June 3, 2013 2:10:16 PM UTC-4, Gary Weaver wrote:
>
> @Dheeraj if you need two associations to the same model with different
> scopes, you could just create another association for each one that is
> needed, e.g.
>
> class Post
> has_many :comments, -> { visible }
> has_many :unscoped_comments, class_name: 'Comment'
> end
>
> However, I wouldn't rely on having multiple associations with different
> scopes, etc. too much. Going beyond scoping, lets consider the
> association's "include" option. Let's say that you use include in the
> association to help avoid n+1 queries that you are seeing. You do this
> because the show method is loading associations. But when you add that, you
> forget that index didn't need those associations. This could significantly
> increase time to perform an index action. So, in the "lets just add a new
> association" mentality, you try to have one association that uses include
> and one that doesn't- except that won't work, because either the
> association is eagerly included or it isn't. So, instead of using include
> on the association in the model, you end up with Comment.all for the index
> method and Comment.where(...).includes(...).first in the show method. Why
> not leave as much as you can in the controller that relates to those
> queries in the individual action methods from the beginning?
>
>
> On Monday, June 3, 2013 11:01:55 AM UTC-4, Dheeraj Kumar wrote:
>>
>> @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].
>> 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.
>>
>>
>> --
>> 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.
>>
>>
>>
--
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.