Ruby on rails Rails 3 ActiveAdmin-用于筛选器选项卡和索引数据的不同查询
我不熟悉ruby、rails和ActiveAdmin 我有一些自定义过滤器,可以返回正确的数据,并且可以在“过滤器”选项卡中看到正确数量的记录 我注意到在索引视图中实际呈现数据时使用了不同的查询,我不知道为什么 经过进一步调查,我认为我的问题与ActiveAdmin中的分页有关。我有一个特定的请求,其中我将自己的可显示广告限制设置为3,但是这个限制被ActiveAdmin中的分页操作覆盖 我尝试将paginate设置为false,但没有任何效果Ruby on rails Rails 3 ActiveAdmin-用于筛选器选项卡和索引数据的不同查询,ruby-on-rails,ruby,ruby-on-rails-3,activeadmin,Ruby On Rails,Ruby,Ruby On Rails 3,Activeadmin,我不熟悉ruby、rails和ActiveAdmin 我有一些自定义过滤器,可以返回正确的数据,并且可以在“过滤器”选项卡中看到正确数量的记录 我注意到在索引视图中实际呈现数据时使用了不同的查询,我不知道为什么 经过进一步调查,我认为我的问题与ActiveAdmin中的分页有关。我有一个特定的请求,其中我将自己的可显示广告限制设置为3,但是这个限制被ActiveAdmin中的分页操作覆盖 我尝试将paginate设置为false,但没有任何效果 # The Query should be # [
# The Query should be
# [ my query is here, see image below ]) ORDER BY RANDOM() LIMIT 3
# Effect with paginate = true to my query is:
# [ my query is here, see image below ]) ORDER BY RANDOM() LIMIT 30 OFFSET 0
config.paginate = true
# Effect with paginate = false to my query is:
# [ my query is here, see image below ]) ORDER BY RANDOM() LIMIT 10000 OFFSET 0
config.paginate = false
# Expected SQL - This is what gets used on the tabs at top of screen
SELECT "advertisements".* FROM "advertisements" WHERE "advertisements"."site_section" = 'Signed In' AND "advertisements"."region_id" IN (10) AND (active = 'true' AND start_date <= '2014-12-01 13:00:00.000000' AND end_date >= '2014-12-01 13:00:00.000000') ORDER BY RANDOM() LIMIT 3
# Actual SQL - This is what is used to render the index
SELECT "advertisements".* FROM "advertisements" WHERE "advertisements"."site_section" = 'Signed In' AND "advertisements"."region_id" IN (10) AND (active = 'true' AND start_date <= '2014-12-01 13:00:00.000000' AND end_date >= '2014-12-01 13:00:00.000000') ORDER BY RANDOM() LIMIT 30 OFFSET 0
下面列出的是一个屏幕截图,其中有非常详尽的日志和视觉提示。我还列出了我的型号和ActiveAdmin代码
活动管理代码
模型
发件人:
您可以设置每个资源每页的记录数:
谢谢你的图片这个答案的问题是它是不可配置的,示例中返回的当前广告数量3是来自custom panel的一个查询参数。如何阻止页面与我的查询进行交互。即使关闭它也不能阻止ActiveAdmin将我的调用更改为limit。
ActiveAdmin.register Advertisement do
menu :parent => 'Partner', :label => 'Adverts', :priority => 2
scope :all, :default => true do |rows|
logger.block '1'
if params[:as] == "test_adverts"
rows
else
rows
end
end
scope :live do |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 do |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 do # proc{ params[:as] == "test_adverts" } do
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 do #|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 do |r|
logger.block '6-RAI'
div :for => r do
div do
h3 "#{r.name} - #{r.region.name}"
span link_to(image_tag(r.image_url)), :class => 'pull-right'
hr
end
end
end
# ---------------------------------------------
# Editor
# ---------------------------------------------
form do |f|
f.inputs "Advertisement" do |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 To Specific Pages" do |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 do |r|
attributes_table do
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 do
r.advertisement_category_id
end
end
end
controller do
def scoped_collection
logger.block '7'
logger.pretty_params(params)
Advertisement.includes(:region, :advertisement_category)
end
end
# == 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 = ? AND start_date <= ? AND 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
ActiveAdmin.register Post do
config.per_page = 10
end