Vincent Rodomista
Vincent Rodomista

Reputation: 780

How to create a list

I'm working on a website for managing dog kennels, and have models such as Owner and Patient. I'm trying to implement a list that will be generated twice a day (AM/PM) with feeding information for that shift. A dog can be labeled NPO, which means that they are not listed for feeding that shift. The logic is:

for each Patient |patient|
  if patient !NPO
    add to feed list
  end
end

I'm confused on how to implement it within rails. I generated a scaffold for feed_list, and gave it the quality: has_many :patients. I also gave patient the quality: belongs_to :feed_list. I'm assuming that my code is supposed to go /views/feed_lists/new.html.erb, but I'm not sure. I tried:

<%= Patient.each do |p| %>
  <%= if p.NPO != true %>
   how do I add to feed list?
  <%= end %>
<%= end %>

my 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

Upvotes: 0

Views: 168

Answers (1)

Subtletree
Subtletree

Reputation: 3329

To create a FeedList and add patients to it you could do:

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

This shows purely how to conditionally add patients to a feedlist however without more information I'm not 100% sure if having a FeedList model is the best strategy for you

Upvotes: 1

Related Questions