如何根据模型中的方法过滤或查找模型?
我有一个控制器正在从联系人表中查找一组联系人。目前它看起来像这样:
@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 方法的控制器中编写该条件(如果这是正确的方法)。
谢谢。
I have a controller which is looking for a set of Contacts from the Contacts table. Currently it looks like this:
@contacts = @campaign.contacts.find(:all, :order => "date_entered ASC")
The method in the contact.rb (Model) is this:
def status
return if statuses.empty?
a= statuses.find(:last).status << ' (' << statuses.find(:last).created_at.to_s(:long) << ')'
return a
end
For the most part, if there is a value in the "status", I no longer want to display it in the view.
Right now, status is polymorphic. That might've been a dumb idea, but I wanted the concept of status to apply across different models:
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
#
I am assuming that if I can add that condition to the @contacts instance, that would take care of it. But I don't know how to write that condition in the controller on the .find method (if that is the right way to do it).
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设
contact has_many statuses
。我不确定应用程序的其余部分,但从您上面给出的代码来看,您似乎只对最后一个状态感兴趣,因此您最好制作状态和属性而不是has_many 关联。
但是,假设对于其他一些要求,您确实需要 has_many 关联,在这种情况下,您所做的似乎是合理的。
I am assuming that
contact has_many statuses
. I am not sure about the rest of the app , but from the code that you have given above , it seems that you are interested in only the last status , and thus you might be better off making status and attribute instead of ahas_many
association .However , assuming that for some other requirement you do need the has_many association , in that case what you have done seems reasonable .
为了扩展 NM 的答案,为什么不建立联系 has_onelatest_status 关系,然后使用 :join 选项查找呢?它记录在 Active Record Base 页面 http://api.rubyonrails.org/classes /ActiveRecord/Base.html,只需搜索:joins。
如果你搜索连接表,并且你已经将关系定义为最新状态,那么你就会得到你想要的,对吗?
To expand on NM's answer, just why not make a contact has_one latest_status relationship, and then find using the :join option? It's documented in the Active Record Base page at http://api.rubyonrails.org/classes/ActiveRecord/Base.html, just search for :joins.
If you search the joined table, and you've defined the relationship already to be the latest status, then you'd have what you want, right?