ruby on rails - ActiveRecord query using 'includes' sometimes applies the default_scope, and sometimes doesn't -
i trying perform activerecord query on user model uses includes
eager-load users' roles , locations. (a user has many roles , has many locations) location model has default scope. is:
where(:status => [0, 2])
when perform simple query includes
, default scope on location model applied. example, query:
user.includes(:roles, :locations)
will contain following in sql output:
select "locations".*, "t0"."user_id" ar_association_key_name "locations" inner join "locations_users" "t0" on "locations"."id" = "t0"."location_id" "locations"."status" in (0, 2)
however, more complex query, such as:
user.includes(:roles, :locations).where(roles: {id: 13})
does not apply location default scope. locations status of 1
being included in users' returned locations.
i notice first query performed multiple select
statements, whereas 2nd query performed 1 select
statement many join
s. how can make default_scope applied in cases?
i using rails 3.2.22.
Comments
Post a Comment