2017-07-25 39 views
0

我有可以在塊中的帳戶。如何通過計算值添加過濾器到activeadmin?

account.rb 
has_many :blocks 

block.rb 
belongs_to :account 

塊模型領域blocked_atunblocked_at。我需要過濾X天以上(含)的賬戶。爲了選擇當前狀態,我的account型號中有字段blocked。 爲了顯示有多少天帳戶已被阻止我加入這AA表:

column 'Days blocked' do |account| 
     (Date.today - account.blocks.last.blocked_at.to_date).to_i if account.blocked 
end 

的問題是如何通過這個領域現在篩選呢?

+0

你看看這個[Nikil Gupta的文章](http://nikhgupta.com/code/activeadmin/custom-filters-using-ransacker-in-activeadmin-interfaces/)? –

回答

0

我相信你可以創建自定義的範圍,像這樣:

scope :scope_name, -> { where('Days blocked' > X) } 

scope_name是不管你的名字和X是無論多少天,你願意的話)

您需要這使洗劫知道你

def self.ransackable_scopes(auth_object = nil) 
    [:scope_name] 
end 

而在你的控制器:可以在你的控制器使用範圍

@accounts = @q.result.scope_name 

注:這將這個範圍內的所有時間過濾器,如果要篩選,只有當用戶點擊一個按鈕或選擇過濾器,看看這個問題:Rails filter with button or link_to using Ransack

+0

感謝您的關注,但我需要動態過濾器。例如,我應該能夠找到被封鎖超過10天的帳戶。 – nobilik

+0

我相信應該爲此工作,如果「被阻止的天數」是AA表中動態更新的列。 – Chase

相關問題