Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count_resultset_without_paging_without_user_filters with include options #83

Closed
MartinHinz opened this issue Jan 16, 2013 · 9 comments
Closed
Assignees

Comments

@MartinHinz
Copy link

Could you please put in the wice_grid.rb in method form_ar_options() the request for the total pages after :joins and :include setting of @ar_options.

Background: I have an external condition parameter that limits the resultset on basis of attributes from joined tables that are not displayed in the grid themself. Wenn the method "count_resultset_without_paging_without_user_filters" is called, the include of this table is not set. This results in an "PGError: ERROR: missing FROM-clause entry for table xxx".

The method would than be:

def form_ar_options(opts = {})  #:nodoc:

  return if @ar_options_formed
  @ar_options_formed = true unless opts[:forget_generated_options]

  # validate @status[:order_direction]
  @status[:order_direction] = case @status[:order_direction]
  when /desc/i
    'desc'
  when /asc/i
    'asc'
  else
    ''
  end

  # conditions
  if @table_column_matrix.generated_conditions.size == 0
    @status.delete(:f)
  end

  @ar_options[:conditions] = klass.send(:merge_conditions, @status[:conditions], * @table_column_matrix.conditions )
  # conditions processed

  if (! opts[:skip_ordering]) && @status[:order]
    @ar_options[:order] = add_custom_order_sql(complete_column_name(@status[:order]))

    @ar_options[:order] += ' ' + @status[:order_direction]
  end

  @ar_options[:joins]   = @options[:joins]
  @ar_options[:include] = @options[:include]
  @ar_options[:group] = @options[:group]

  if self.output_html?
    @ar_options[:per_page] = if all_record_mode?
      # reset the :pp value in all records mode
      @status[:pp] = count_resultset_without_paging_without_user_filters
    else
      @status[:per_page]
    end

    @ar_options[:page] = @status[:page]
    @ar_options[:total_entries] = @status[:total_entries] if @status[:total_entries]
  end
end
@leikind
Copy link
Owner

leikind commented Jan 16, 2013

I am not sure I understand how your code works and why :total_entries should be added to form_ar_options.

You say :

Wenn the method "count_resultset_without_paging_without_user_filters" is called, the include of this table is not set.

Why is the table not joined?
count_resultset_without_paging_without_user_filters takes :joins and :include into account: https://github.com/leikind/wice_grid/blob/rails3/lib/wice_grid.rb#L545

Maybe the client code could make things clearer (initialize_grid)

@MartinHinz
Copy link
Author

The controller code looks like this:

@samples_grid = initialize_grid(Sample.with_permissions_to(:show),
:include => [:lab, :prmat, :feature_type, {:site=>{:country_subdivision => :country}}, {:phase => :culture}, :literatures],
:name => 'samples',
:conditions=>scope,
:enable_export_to_csv => true,
:csv_field_separator => ';'
)

In ":conditions => scope" I enter my preformed conditions. This can include eg. country.id as condition.

The problem appears when form_ar_options is called first time by another method (I am not sure what method calls it, btw). It is processed until

  if self.output_html?
    @ar_options[:per_page] = if all_record_mode?
      # reset the :pp value in all records mode
      @status[:pp] = count_resultset_without_paging_without_user_filters
      ...

At this stage @ar_options[:include] is not set, but @ar_options_formed is already set to "true", since this takes place at the beginning of the method:

def form_ar_options(opts = {})  #:nodoc:

  return if @ar_options_formed
  @ar_options_formed = true unless opts[:forget_generated_options]

"count_resultset_without_paging_without_user_filters" itself triggers form_ar_options(), but this returns since @ar_options_formed is set to true. So @ar_options[:include] is not set when the rest of the method takes places:

def count_resultset_without_paging_without_user_filters  #:nodoc:
  form_ar_options
  @klass.unscoped do
    @relation.count(
      :joins => @ar_options[:joins],
      :include => @ar_options[:include],
      :group => @ar_options[:group],
      :conditions => @options[:conditions]
    )
  end
end

Hope, this clarifies the issue, or is there an error in my use of the method?

@leikind
Copy link
Owner

leikind commented Jan 16, 2013

Could you also please post the structure in scope used in a request that leads to the exception, and the complete exception stacktrace?

@MartinHinz
Copy link
Author

Full Trace:

activerecord (3.1.0) lib/active_record/connection_adapters/postgresql_adapter.rb:962:in `async_exec'
activerecord (3.1.0) lib/active_record/connection_adapters/postgresql_adapter.rb:962:in `exec_no_cache'
activerecord (3.1.0) lib/active_record/connection_adapters/postgresql_adapter.rb:551:in `block in exec_query'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
activerecord (3.1.0) lib/active_record/connection_adapters/postgresql_adapter.rb:550:in `exec_query'
activerecord (3.1.0) lib/active_record/connection_adapters/postgresql_adapter.rb:1031:in `select'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `block in select_all'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/query_cache.rb:75:in `cache_sql'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_one'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:30:in `select_value'
activerecord (3.1.0) lib/active_record/relation/calculations.rb:222:in `execute_simple_calculation'
activerecord (3.1.0) lib/active_record/relation/calculations.rb:187:in `perform_calculation'
activerecord (3.1.0) lib/active_record/relation/calculations.rb:155:in `calculate'
activerecord (3.1.0) lib/active_record/relation/calculations.rb:147:in `calculate'
activerecord (3.1.0) lib/active_record/relation/calculations.rb:58:in `count'
wice_grid (3.2.0) lib/wice_grid.rb:546:in `block in count_resultset_without_paging_without_user_filters'
activerecord (3.1.0) lib/active_record/base.rb:936:in `block in unscoped'
activerecord (3.1.0) lib/active_record/relation.rb:180:in `block in scoping'
activerecord (3.1.0) lib/active_record/base.rb:1190:in `with_scope'
activerecord (3.1.0) lib/active_record/relation.rb:180:in `scoping'
activerecord (3.1.0) lib/active_record/base.rb:936:in `unscoped'
wice_grid (3.2.0) lib/wice_grid.rb:545:in `count_resultset_without_paging_without_user_filters'
wice_grid (3.2.0) lib/wice_grid.rb:268:in `form_ar_options'
wice_grid (3.2.0) lib/wice_grid.rb:285:in `read'
wice_grid (3.2.0) lib/wice_grid.rb:325:in `resultset'
wice_grid (3.2.0) lib/helpers/wice_grid_view_helpers.rb:628:in `pagination_info'
wice_grid (3.2.0) lib/helpers/wice_grid_view_helpers.rb:585:in `pagination_panel_content'
wice_grid (3.2.0) lib/helpers/wice_grid_view_helpers.rb:339:in `block in grid_html'
wice_grid (3.2.0) lib/grid_renderer.rb:99:in `pagination_panel'
wice_grid (3.2.0) lib/helpers/wice_grid_view_helpers.rb:334:in `grid_html'
wice_grid (3.2.0) lib/helpers/wice_grid_view_helpers.rb:126:in `define_grid'
wice_grid (3.2.0) lib/helpers/wice_grid_view_helpers.rb:76:in `grid'
app/views/samples/_samples_grid.html.erb:1:in `_app_views_samples__samples_grid_html_erb__4136256380941846993_70309435207660'
actionpack (3.1.0) lib/action_view/template.rb:144:in `block in render'
activesupport (3.1.0) lib/active_support/notifications.rb:55:in `instrument'
actionpack (3.1.0) lib/action_view/template.rb:142:in `render'
actionpack (3.1.0) lib/action_view/renderer/partial_renderer.rb:256:in `render_partial'
actionpack (3.1.0) lib/action_view/renderer/partial_renderer.rb:228:in `block (2 levels) in render'
actionpack (3.1.0) lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.0) lib/action_view/renderer/abstract_renderer.rb:33:in `instrument'
actionpack (3.1.0) lib/action_view/renderer/partial_renderer.rb:227:in `block in render'
actionpack (3.1.0) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats'
actionpack (3.1.0) lib/action_view/renderer/partial_renderer.rb:219:in `render'
actionpack (3.1.0) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.1.0) lib/action_view/renderer/renderer.rb:15:in `render'
actionpack (3.1.0) lib/action_view/helpers/rendering_helper.rb:24:in `render'
app/views/samples/index.html.erb:111:in `_app_views_samples_index_html_erb___4307503596438024869_70309430255000'
actionpack (3.1.0) lib/action_view/template.rb:144:in `block in render'
activesupport (3.1.0) lib/active_support/notifications.rb:55:in `instrument'
actionpack (3.1.0) lib/action_view/template.rb:142:in `render'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:40:in `block (2 levels) in render_template'
actionpack (3.1.0) lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.0) lib/action_view/renderer/abstract_renderer.rb:33:in `instrument'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:39:in `block in render_template'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:47:in `render_with_layout'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:38:in `render_template'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:12:in `block in render'
actionpack (3.1.0) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:9:in `render'
actionpack (3.1.0) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.1.0) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:120:in `_render_template'
actionpack (3.1.0) lib/action_controller/metal/streaming.rb:250:in `_render_template'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:114:in `render_to_body'
actionpack (3.1.0) lib/action_controller/metal/renderers.rb:30:in `render_to_body'
actionpack (3.1.0) lib/action_controller/metal/compatibility.rb:43:in `render_to_body'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:99:in `render'
actionpack (3.1.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.1.0) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/martin/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.1.0) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.1.0) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.1.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.1.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.0) lib/active_support/callbacks.rb:443:in `_run__2623133773019009823__process_action__179673600646436902__callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.0) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:531:in `call'
galetahub-simple_captcha (0.1.3) lib/simple_captcha/middleware.rb:20:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.6) lib/rack/etag.rb:23:in `call'
rack (1.3.6) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/cookies.rb:326:in `call'
activerecord (3.1.0) lib/active_record/query_cache.rb:62:in `call'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.0) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.0) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.0) lib/rails/engine.rb:455:in `call'
railties (3.1.0) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.0) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
/home/martin/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/martin/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/martin/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Request Parameters:

{"scope"=>{"countries.id"=>"11"},
 "samples"=>{"order_direction"=>"",
 "pp"=>"92"}}

scope in this case: ["countries.id = ?", "11"]

@leikind
Copy link
Owner

leikind commented Jan 16, 2013

Ok, I have found a logical error in my code. It will be fixed in 3.2.1.pre2

@ghost ghost assigned leikind Jan 16, 2013
@leikind
Copy link
Owner

leikind commented Jan 16, 2013

Thanx for helping find it!

@MartinHinz
Copy link
Author

Thank you a lot for taking the time helping me out, and btw. for this wonderful peace of code!

@leikind
Copy link
Owner

leikind commented Jan 21, 2013

Please verify if version 3.2.1.pre3 fixes the problem http://rubygems.org/gems/wice_grid/versions/3.2.1.pre3

@MartinHinz
Copy link
Author

Thank you very much, the fix solves the problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants