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: