I have a scope defined in my model (say User) as
user.rb
scope :dummy_filter, lambda { |opts={}|
options = { ftr:{}, page: 1, per_page: 50 }
opts = options.merge opts
filter(
params: {query_hash: opts[:ftr], page: opts[:page], wf_per_page: opts[:per_page] },
filter: :custom_filter
)
}
Now, when I access this scope I get two queries as,
> User.dummy_filter
User Load (12.7ms) SELECT `users`.* FROM `users` ORDER BY users.id desc LIMIT 50 OFFSET 0
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`active` = 0 AND `users`.`account_id` = 1 ORDER BY users.id desc LIMIT 50 OFFSET 0
But when I use the will_filter directly I don't get the first query, I just get
> User.filter(params: {query_hash: opts[:ftr], page: opts[:page], wf_per_page: opts[:per_page] }, filter: :custom_filter)
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`active` = 0 AND `users`.`account_id` = 1 ORDER BY users.id desc LIMIT 50 OFFSET 0
which is the expected result. The same expected result can be seen when I define a class method similar to the scope in the user model.
def self.dummy_filter(opts={})
options = { ftr:{}, page: 1, per_page: 50 }
opts = options.merge opts
filter(
params: {query_hash: opts[:ftr], page: opts[:page], wf_per_page: opts[:per_page] },
filter: :custom_filter
)
end
In this case also I get the expected result.
> User.dummy_filter
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`active` = 0 AND `users`.`account_id` = 1 ORDER BY users.id desc LIMIT 50 OFFSET 0
My question is why is it misbehaving in Rails scope...or is it the intended behaviour?
Well, on debugging through the gem, I've tracked the problem down to these two lines (893,894) in /app/models/will_filter/filter.rb :
recs = recs.page(page).per(per_page)
recs.wf_filter = self
the line recs.wf_filter = self in particular. When I remove this line even the scope is behaving as expected. Not aware of after effects, if I remove this line.
Can someone throw some light over this issue, if it really is?
I have a scope defined in my model (say User) as
Now, when I access this scope I get two queries as,
But when I use the will_filter directly I don't get the first query, I just get
which is the expected result. The same expected result can be seen when I define a class method similar to the scope in the user model.
In this case also I get the expected result.
My question is why is it misbehaving in Rails scope...or is it the intended behaviour?
Well, on debugging through the gem, I've tracked the problem down to these two lines (893,894) in /app/models/will_filter/filter.rb :
the line
recs.wf_filter = selfin particular. When I remove this line even the scope is behaving as expected. Not aware of after effects, if I remove this line.Can someone throw some light over this issue, if it really is?