ruby on rails - How can I override a default scope in a has_many :through association? -


override default scope isn't being persisted (in useful way) in has_many :through association.

here's relevant stuff models:

class user   has_one :invitation   default_scope where(registered: true) end  class invitation    belongs_to :user, conditions: { registered: [true, false] }    belongs_to :program end  class program   has_many :invitations   has_many :users, through: :invitations end 

finding users through invitations works expected.

invitation.joins(:user) 

generates this:

select `invitations`.* `invitations` inner join `users` on `users`.`id` = `invitations`.`user_id` , `users`.`registered` in (1, 0) 

given this, i'd assume program.last.users should find relevant users, whether registered true or false. instead, this:

select `users`.* `users` inner join `invitations` on `users`.`id` = `invitations`.`user_id` `users`.`registered` = 1 , `invitations`.`program_id` = 52 , (`users`.`registered` in (1, 0)) 

this sql expect. how create association gives me this?

select `users`.* `users` inner join `invitations` on `users`.`id` = `invitations`.`user_id` `users`.`registered` in (1, 0) , `invitations`.`program_id` = 52 

more context: i'm using rails 3.1.3, , upgrading rails isn't viable option before feature needs go out. getting rid of default_scope isn't thing can either.

class program   # override association method   def users     program.joins(:invitation).joins(:users).where('users.registered = true')....etc   end end 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -