I came across this exact same behavior about three months ago.

If every other access to an AR class with a default scope honors that scope in 
the generated SQL, why not in a join situation?

Wes

Sent from my iPhone

On Jun 15, 2013, at 9:37 AM, soffolk Zhu <[email protected]> wrote:

> Hi, guys
> 
> I have a puzzle about default_scope.
> 
> Suppose I have two model:
> 
> class User < ActiveRecord::Base
>   has_many :blogs
>   default_scope where(deleted_at: nil)
> end
> 
> class Blog < ActiveRecord::Base
>   belongs_to :user
> end
> I want to produce sqlselect blogs.* from blogs inner join users on users.id = 
> blogs.user_id and users.deleted_at is null
> 
> And the code Blog.joins(:user), which I think is good, produceselect blogs.* 
> from blogs inner join users on users.id = blogs.user_id
> 
> The default_scope is not appended to the join condition, why?
> 
> To reach my goal, I must add conditions to belongs_to:
> 
> class Blog < ActiveRecord::Base
>   belongs_to :user, conditions: "deleted_at is null"
> end
> Why not append default_scope to join condition directly as the doc said:
> 
> Use this macro in your model to set a default scope for all operations on the 
> model.
> -- 
> 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.
> 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.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to