我正在为管理狗舍的网站工作,并有模型,如主人和病人。我正在尝试实现一个列表,该列表将每天生成两次(AM/PM),并提供该班次的信息。一只狗可以被标记为NPO,这意味着它们不会被列入喂养该班次的名单。逻辑是这样的:
for each Patient |patient|
if patient !NPO
add to feed list
end
end
我对如何在rails中实现它感到困惑。我为feed_list
生成了一个脚手架,并赋予它质量:has_many :patients
。我也给了patient
质量:belongs_to :feed_list
。我假设我的代码应该是/views/feed_lists/new.html.erb
的,但我不确定。我试过了:
<%= Patient.each do |p| %>
<%= if p.NPO != true %>
how do I add to feed list?
<%= end %>
<%= end %>
我的Patient.rb
class Patient < ActiveRecord::Base
belongs_to :owner, :feed_list
validates :name, presence: true, length: { maximum: 50 }
has_many :stays
end
feed_list.rb
class FeedList < ActiveRecord::Base
has_many :patients
end
发布于 2015-11-26 21:09:25
要创建FeedList并向其中添加患者,您可以执行以下操作:
f = FeedList.new
f.patients << Patient.where(NPO: false).first
# or to add several patients at once
# f.patient_ids= Patient.where(NPO: false).map(&:id)
f.save
这仅仅展示了如何有条件地将患者添加到馈送列表,但是没有更多的信息,我不能百分之百确定拥有feedlist模型是否是最适合您的策略
https://stackoverflow.com/questions/33946167
复制相似问题