ActiveAdmin.rb
中的以下類方法將根據條件返回所有記錄,例如, ActiveAdmin.current("Yes")
或ActiveAdmin.current("No")
。
def self.current(inp = "Yes") # default inp is "Yes"
d = Time.zone.now.to_date
return case inp
# when inp == "Yes" it would return all the records with archived == false (won't return archived == nil)
# AND parents with start_date <= d and end_date >= d
when "Yes"
where(archived: false).
joins(:parent).
where("parents.start_date <= ? AND parents.end_date >= ?",d,d)
# when inp == "No" it would return all the records with archived == true (won't return archived == nil)
# AND parents with start_date > d OR end_date < d
when "No"
where(archived: true).
joins(:parent).
where("parents.start_date > ? OR parents.end_date < ?",d,d)
# when inp = "Any" return all records
when "Any"
scoped
# return all records if inp does not match any of the above options
else
scoped
end
end
你能否提供關於'conditions'的更多細節。首先想到的是使用'範圍',但需要看看條件是否會起作用。 – tihom
還請指定您正在過濾的是記錄還是視圖或方法調用? – engineersmnky
@tihom和engineersmnky - 請參閱上面的更新。 –