reports_kit
reports_kit copied to clipboard
ActiveRecord Associations
In my database I have a store, each store has_many locations, and each location has_many assets. I'm needing to use a context filter based on the current_user's store id, which I have working in more straightforward associations, but I need help with this one. How can I get assets that are only associated with the current user's store_id?
I'm currently getting this error: NoMethodError (undefined method `klass' for nil:NilClass)
<%= render_report 'my_assets_by_age', context_params: { chain_store_id: current_user.chain_store_id } do |report| %>
asset.rb
include ReportsKit::Model reports_kit do contextual_filter :for_chain_store, ->(relation, context_params) { relation.where(chain_store: context_params[:chain_store_id]) } end
Hi The best way to go about this is to use your own data method. There are some good examples in the documentation that allow you to do something like this. (https://www.reportskit.co/examples/data_methods_two_dimensions) If you need help writing a custom query for your use case let me know. I hope it helps