2016-01-28 66 views
0

我使用活動管理與ActiveRecord範圍。不過,我在添加範圍時遇到了問題。活動管理範圍不起作用1.0.0.pre2

運行紅寶石2.2.1p85(2015年2月26日修訂49769)x86_64的Linux的]和 Rails的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: /\A\d{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 

我厭倦了它使用它和無塊。我希望該範圍內的「程序」總數作爲最終結果。

+0

儘管我從來沒有使用過它,但我已經看過它的文檔,並且似乎模型完全由框架管理。和「範圍」一詞可以自己衝突 「範圍」,一個過濾器屏幕。我總是避免使用框架或插件,如果我可以自己做 –

回答

0

您不能有兩個默認範圍,並且不需要scope :all,因此請將其刪除。

你有這樣的範圍也很正常

scope :program_name, -> (program) {where(program_name: adult) } 

和你說

我希望能夠給我們在這個應用程序/管理/ account.rb

但你並沒有真正使用它。你是不是嘗試使用

scope :adult, default: true do |accounts| 
    accounts.program_name('adult') 
end 

所以只是將它加入

scope :program_name 

但你的問題似乎裝有別的東西,你現在要做的

我希望該範圍內「程序」的總數作爲最終結果。

而在這個意義上,我認爲你可能會誤解scopes的實際用途。