我正在使用带有ActiveRecord范围的活动管理员。但是,我在添加范围时遇到问题。
运行 ruby 2.2.1p85 (2015-02-26 修订版 49769) [x86_64-linux] 和导轨 4.2.5.1
#app/model/accounts.rb
class Account < ActiveRecord::Base
searchkick
belongs_to :program
belongs_to :insurance
has_many :notes
scope :program_name, -> (program) {where(program_name: adult) }
validates :first_name, :last_name, :address, :phone, presence: true
validates :phone, format: { with: /Ad{3} d{3}-d{4}z/,
message: "must be in the format 123 456-7890" }
end
我希望能够在应用程序/管理员/帐户.rb 中做到这一点
#app/admin/account.rb
ActiveAdmin.register Account do
menu :priority => 2
permit_params :first_name, :last_name, :return_client, :program_id, :insurance_id, :address, :phone
index do
column :first_name
column :last_name
column :address
column :phone
column :created_at
column :return_client
column :program
column :insurance
actions
end
scope :all, :default => true
scope :adult, default: true do |accounts|
accounts.program_name('adult')
end
end
我厌倦了在有和没有块的情况下使用它。我希望该范围内的"程序"总数作为最终结果。
您不能有两个默认作用域,并且不需要scope :all
,因此请将其删除。
你有这个范围看起来不错
scope :program_name, -> (program) {where(program_name: adult) }
你说
我希望能够在应用程序/管理员/帐户.rb 中做到这一点
但你实际上并没有使用它。相反,您正在尝试使用
scope :adult, default: true do |accounts|
accounts.program_name('adult')
end
所以只需添加它
scope :program_name
但是您的问题似乎加载了您要做的其他事情
我希望该范围内的"程序"总数作为最终结果。
从这个^意义上说,我认为您可能误解了作用域的实际用途和用途。