0

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
sawa
  • 165,429
  • 45
  • 277
  • 381
  • a clue : `FeedList.first.patients.each` .... – charlysisto Nov 26 '15 at 20:18
  • ok, but how do I add to FeedList.first? – Vincent Rodomista Nov 26 '15 at 20:43
  • As in your question, your feed list should be generated twice a day. How do you want to control it, manually or automatically? And when it generates a new feed list, how will you deal with the old one, delete it or keep history? Please clarify it since it depends on how you design it. – Yang Nov 26 '15 at 21:07
  • have you created a feed_list model? – 0r4cl3 Nov 26 '15 at 21:08
  • I assume that I have created a feed_list model, as I generated the feed_list through a scaffold. @Yang, I want to manually generate the feedlist with a button on patients#index. I would like to keep feed lists in history for two weeks. – Vincent Rodomista Nov 26 '15 at 21:14

1 Answers1

1

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

charlysisto
  • 3,700
  • 17
  • 30
Subtletree
  • 3,169
  • 2
  • 18
  • 25
  • thanks! what do you think would be a preferred strategy to having a FeedList model? I'm open to any suggestions – Vincent Rodomista Nov 26 '15 at 21:12
  • It depends how you want to use the FeedList. If you just want to be able to get a list of Patients at any time who are not NPO then you can just use `Patient.where(NPO: false)` for example and that would be your feedlist. – Subtletree Nov 26 '15 at 21:15
  • wow, never thought of it like that. I'm also having a lot of trouble figuring out how to format the list in a readable way; since I only want to be able to see certain elements of Patient (Name, diet) and not everything. Any idea where I should start as far as trying to figure that out? – Vincent Rodomista Nov 26 '15 at 21:17
  • Does this data get displayed in a FeedList 'show' view? In that view you would just only display the information you want i.e `<%= patient.name %>` As a side note if you're only just starting with rails it can often be faster to do a [tutorial](https://www.railstutorial.org/book) and then come back to your project. – Subtletree Nov 26 '15 at 21:21
  • thanks again! I'm actually using Hartl's RORT as a baseline for my project, and it's what I'm currently referencing – Vincent Rodomista Nov 26 '15 at 21:26
  • ok great! Good luck with the project! I noticed one other thing too, in your code you have `<%= end %>` for example. The `=` there means that whatever is returned from the enclosed code should be displayed in the html. You would use that for things like `<%= patient.name %>`. In the erb code you pasted above though you have 4 `=` all of which are unnecessary (e.g. should just be `<% end %>`) and will output undesired things to the rendered page. [more info here](http://stackoverflow.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails) – Subtletree Nov 26 '15 at 21:33