Well, by changing apply_scopes to be this:
def apply_scopes(scopes)
binding.pry
result = self
scopes.each_pair do |scope, arg|
if arg.is_a?(Array)
result = result.send(scope, *arg) unless arg.first.blank? #query
against existing result instead of self
else
result = result.send(scope, arg) unless arg.blank?
end
end
result
end
I get the query I was expecting:
[4] crs(Entity) ยป result.to_sql
=> "SELECT [entities].* FROM [entities] WHERE (entities.status =
N'in_compliance') AND (entities.entity_type = N'cooperative')"
Hooray!
So, can anyone see a downside fixing apply_scopes as such? I'm tempted to
submit a pull request!
Tim
On Friday, June 12, 2015 at 6:19:28 PM UTC-4, Tim Griffin wrote:
>
> Yes, I can probably do it that way, but I was trying to take advantage of
> Hobo's built-in scopes.
>
> In looking closer at HObo's *apply_scopes* method, I see that each time
> it iterates, it does not further refine *result*. It just keeps querying
> the original model, *self*:
>
> def apply_scopes(scopes)
> binding.pry
> result = self
> scopes.each_pair do |scope, arg|
> if arg.is_a?(Array)
> result = self.send(scope, *arg) unless arg.first.blank?
> else
> result = self.send(scope, arg) unless arg.blank?
> end
> end
> result
> end
>
> So, that's the problem.
>
> There needs to be a check that if there is already a result for the next
> iteration of a *scope*, then apply the scope to the existing result, not
> to "self".
>
> Will tinker this a bit more, but that's my theory.
>
> Tim
>
>
>
> On Thursday, June 11, 2015 at 10:26:48 PM UTC-4, Tim Griffin wrote:
>>
>> Hi all;
>>
>> Perhaps I'm missing something, but I was expecting two filter parameters
>> to become part of a query through scopes.merge! But, one seems to be
>> dropped.
>>
>> From an index page, I have two filter combos that will form a query such
>> as this:
>>
>> http://localhost:3000/admin/entities?
>> status=active&entity_type=corporation
>>
>> In my #index controller method, I have
>>
>> scopes = {
>>
>> search: [params[:search], :fileno, :name],
>> order_by: parse_sort_param(:id, :fileno, :name)
>> }
>> scopes.merge!(status_is: params[:status])
>> scopes.merge!(entity_type_is: params[:entity_type])
>>
>> But, the resulting query that gets fed to PostgreSQL is this:
>>
>> [INFO ] GET "/admin/entities?status=active&entity_type=corporation"
>> for 127.0.0.1 at 2015-06-11 22:20:19 -0400
>> [DEBUG] SQL (0.6ms) USE [YCOR_DEV]
>> [INFO ] === Processing by Admin::EntitiesController#index as HTML
>> [INFO ] Parameters: {"status"=>"active", "entity_type"=>"corporation"}
>> DEBUG] Entity Load (7701.7ms) EXEC sp_executesql N'SELECT TOP (50)
>> [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY fileno DESC) AS [__rn],
>> [entities].* FROM [entities] WHERE (entities.entity_type =
>> N''corporation'') ) AS [__rnt] WHERE [__rnt].[__rn] > (0) ORDER BY
>> [__rnt].[__rn] ASC'
>>
>>
>> There's no sign of the *status* parameter.
>>
>> Am I missing something basic here?
>>
>> Thanks,
>> Tim
>>
>>
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" 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/hobousers.
For more options, visit https://groups.google.com/d/optout.