ruby - Rails 3 ActiveAdmin - different queries used for the filter tab vs the index data -


i'm new ruby, rails, activeadmin.

i have custom filters return correct data , can see correct number of records appearing in filter tab.

i notice different query used render data in index view , don't know why.

with further investigation, think issue related paging in activeadmin. have specific request have own limit of displayable adverts set three, limit being overridden paging operation within activeadmin.

i tried setting paginate false, has not had effect.

# query should # [ query here, see image below ]) order random() limit 3  # effect paginate = true query is: # [ query here, see image below ]) order random() limit 30 offset 0 config.paginate = true  # effect paginate = false query is: # [ query here, see image below ]) order random() limit 10000 offset 0 config.paginate = false   # expected sql - gets used on tabs @ top of screen select "advertisements".* "advertisements" "advertisements"."site_section" = 'signed in' , "advertisements"."region_id" in (10) , (active = 'true' , start_date <= '2014-12-01 13:00:00.000000' , end_date >= '2014-12-01 13:00:00.000000') order random() limit 3   # actual sql - used render index select "advertisements".* "advertisements" "advertisements"."site_section" = 'signed in' , "advertisements"."region_id" in (10) , (active = 'true' , start_date <= '2014-12-01 13:00:00.000000' , end_date >= '2014-12-01 13:00:00.000000') order random() limit 30 offset 0 

listed below screenshot pretty thorough log , visual cues. have listed model , activeadmin code.

log , visual issues

activeadmin code

activeadmin.register advertisement   menu :parent => 'partner', :label => 'adverts', :priority => 2    scope :all, :default => true |rows|     logger.block '1'     if params[:as] == "test_adverts"         rows     else         rows     end   end    scope :live |rows|     logger.block '2'     logger.kv 'sql', advertisement.get_ads_by_location_aa(params, 0).to_sql     logger.kv 'count', advertisement.get_ads_by_location_aa(params, 0).count     advertisement.get_ads_by_location_aa(params, 0)   end   scope :next_month |rows|     logger.block '3'     advertisement.get_ads_by_location_aa(params, 1)   end    filter :staffrooms   filter :advertisement_category   filter :region   filter :active   filter :name   filter :start_date   filter :end_date   filter :is_workible   filter :site_section, :as => :check_boxes, :collection => proc { advertisement::site_section_types }   filter :by_loco, label: "location xxx", as: :string, :class => "select-location" #, collection: %w[ new active inactive ]    sidebar :by_location, :if => true # proc{ params[:as] == "test_adverts" }     logger.block '4'     render partial: "index"   end      # ---------------------------------------------     # internal helper classes     # ---------------------------------------------    class renderadvertisementindex < activeadmin::views::indexasblock     def self.index_name         pl.block '5'       "test_adverts"     end   end       # ---------------------------------------------     # index pages     # ---------------------------------------------      index #|html|         logger.block '6-index'         selectable_column          default_actions_extended          column_bool_yn :active         column :region         column :advertisement_category, :label => 'category'         column :site_section, :label => 'section'         column :name         column :image_url         column_bool_yn :is_workible         column_date_ddmmyy :start_date         column_date_ddmmyy :end_date     end    index as: renderadvertisementindex |r|     logger.block '6-rai'      div :for => r       div         h3 "#{r.name} - #{r.region.name}"         span link_to(image_tag(r.image_url)), :class => 'pull-right'         hr       end     end   end      # ---------------------------------------------     # editor     # ---------------------------------------------      form |f|     f.inputs "advertisement" |advertisement|             f.input :active         f.input :region_id, as: :select, :collection => region.all, :include_blank => false         f.input :advertisement_category_id, as: :select, :collection => advertisementcategory.all, :include_blank => false, :label => 'category'         f.input :site_section, :as => :radio, :collection => advertisement::site_section_types, :label => 'section'         #f.input :site_section, :as => :radio, :collection => advertisement::site_section_types.map { |e| [e, e]  }, :label => 'section'         #f.input :site_section, :as => :radio, :collection => [["male", false], ["female", true]], :label => 'section'         f.input :name         f.input :image_url         f.input :target_url         f.input :start_date         f.input :end_date         f.input :is_workible, :label => "is workible advert"     end      f.inputs "filter specific pages" |advertisement|         f.input :staffrooms, :label => "filter on industry pages", :collection => staffroom.where(staffroom_type: 'industry'), :as => :check_boxes         f.input :staffrooms, :label => "filter on communities pages", :collection => staffroom.where(staffroom_type: 'site'), :as => :check_boxes     end          f.buttons     end      # ---------------------------------------------     # viewer     # ---------------------------------------------      show |r|     attributes_table         row :active         row :region         row :advertisement_category         row :site_section         row :name         row :image_url         row :target_url         row :start_date         row :end_date         row :is_workible         row :category             r.advertisement_category_id         end     end     end      controller      def scoped_collection       logger.block '7'       logger.pretty_params(params)        advertisement.includes(:region, :advertisement_category)     end  end 

model

# == schema information # # table name: advertisements # #  id           :integer          not null, primary key #  active       :boolean #  name         :string(255) #  content      :string(255) #  image_url    :string(255) #  target_url   :string(255) #  created_at   :datetime         not null #  updated_at   :datetime         not null #  region_id    :integer #  site_section :string(255) #  start_date   :date #  end_date     :date #  is_workible  :boolean # class advertisement < activerecord::base   after_initialize :init   attr_accessible :active, :name, :content, :image_url, :target_url, :advertisement_category_id, :region_id, :site_section, :start_date, :end_date, :is_workible, :staffroom_ids    #----------------------------------------------------------------------------------------------------   # relationships   #----------------------------------------------------------------------------------------------------    has_and_belongs_to_many :staffrooms   belongs_to :advertisement_category   belongs_to :region    #----------------------------------------------------------------------------------------------------   # constants   #----------------------------------------------------------------------------------------------------    site_section_types = ['public', 'signed in']    #----------------------------------------------------------------------------------------------------   # validations   #----------------------------------------------------------------------------------------------------    validates :name, presence: true   validate :custom_validation    def custom_validation     self.image_url = applyhttp(self.image_url)     self.target_url = applyhttp(self.target_url)   end    def applyhttp(url)     unless url[/\ahttp:\/\//] || url[/\ahttps:\/\//]         return "http://#{url}"     end     return url   end   #----------------------------------------------------------------------------------------------------   # queries   #----------------------------------------------------------------------------------------------------    def self.live(month_offset = 0)     date = ::time.zone.now.midnight+month_offset.month     where(["active = ? , start_date <= ? , end_date >= ?", :true, date, date])#.includes(:region,:advertisement_category)   end    #----------------------------------------------------------------------------------------------------   # sample - cremorne point   #   # advertisement.get_ads_by_location('signed in',4921,5).pluck_all('id, name, site_section')    #   #----------------------------------------------------------------------------------------------------   # sample - hellendsburg   #   # advertisement.get_ads_by_location('signed in',7310,5).pluck_all('id, name, site_section')    #   #----------------------------------------------------------------------------------------------------   def self.get_ads_by_location(section, location, month_offset = 0, ad_count = 3)     location = location.find(location)      region_ids = region.near(location.location, 50).pluck(:id)      live().where(site_section: section).where(region_id: region_ids).order("random()").limit(ad_count)   end    #----------------------------------------------------------------------------------------------------   # active admin - accessors   #----------------------------------------------------------------------------------------------------    def self.get_ads_by_location_aa(params, month_offset)     location_id = params["q"]["location_id"] unless params['q'].blank?        location_id = location_id.to_i       advertisement.get_ads_by_location('signed in', location_id, month_offset)   end  end 

from here:

you can set number of records per page per resources:

activeadmin.register post   config.per_page = 10 end 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -