我有一個控制器正在從聯繫人表中查找一組聯繫人。目前,它看起來像這樣:如何根據該模型中的方法過濾或查找模型?
@contacts = @campaign.contacts.find(:all, :order => "date_entered ASC")
在contact.rb(模型)的方法是這樣的:
def status
return if statuses.empty?
a= statuses.find(:last).status << ' (' << statuses.find(:last).created_at.to_s(:long) << ')'
return a
end
在大多數情況下,如果在「狀態」的值,我不再希望在視圖中顯示它。
現在,狀態是多態的。這可能已經是一個愚蠢的想法,但我希望的狀態的概念在不同的模式,以適用於:
class Status < ActiveRecord::Base
attr_accessible :statusable_id, :statusable_type, :status
belongs_to :statusable, :polymorphic => true
end
# == Schema Information
#
# Table name: statuses
#
# id :integer not null, primary key
# statusable_id :integer
# statusable_type :string(255)
# status :string(255)
# created_at :datetime
# updated_at :datetime
#
我假設,如果我可以添加條件到@contacts情況下,這將照顧它。但我不知道如何在.find方法的控制器中寫入該條件(如果這是正確的方式)。
謝謝。
嗨,但我作爲一個條件做什麼? 我不確定我會保持has_many ...我*想*這將允許我跟蹤任何更改作爲狀態更改...但不知道。 我也在考慮通過多態來使多個模型全部成爲狀態......而不是具有單獨的屬性。但如果我這樣做會更簡單。 – Angela 2010-07-09 22:51:12