0

The model Patient belongs_to FeedList, which has_many patients. I'm attempting, upon the creation of a new FeedList I want to add all patients that are currently in the DB to the FeedList. I'm currently attempting to do it within create of feed_lists_controller.rb.

def create
    @feed_list = FeedList.new(feed_list_params)

    Patients.all.each do |p|
      @feed_list.patients << p
    end

    respond_to do |format|
      if @feed_list.save
        format.html { redirect_to @feed_list, notice: 'Feed list was successfully created.' }
        format.json { render :show, status: :created, location: @feed_list }
      else
        format.html { render :new }
        format.json { render json: @feed_list.errors, status: :unprocessable_entity }
      end
    end
  end

however, it doesn't seem to be registering when I create a new FeedList

[8] pry(main)> FeedList.create
=> #<FeedList:0xbaf7bdd0
 id: 3,
 created_at: Sun, 29 Nov 2015 01:11:54 UTC +00:00,
 updated_at: Sun, 29 Nov 2015 01:11:54 UTC +00:00,
 date: nil,
 integer: nil>
[9] pry(main)> FeedList.last.patients
=> #<Patient::ActiveRecord_Associations_CollectionProxy:0x-2284f570>

FeedList.rb:

class FeedList < ActiveRecord::Base
    has_many :patients

after_create :add_patients
  private
    def add_patients
      ::Patients.all.each do |p|
        self.patients << p
      end
    end

end

Am I on the right track?

  • That looks fine to me. – Jason Nov 28 '15 at 21:26
  • pls see edit, it doesnt seem to be working @jason – Vincent Rodomista Nov 28 '15 at 21:28
  • 1
    The create action isn't a callback that gets invoked every time you create an instance of a model, it's a controller action that runs whenever a route is visited that is tied to it. At least, that's how it's usually set up. Where is this create function? – Jason Nov 28 '15 at 21:29
  • it's in the `feed_lists_controller.rb` file – Vincent Rodomista Nov 28 '15 at 21:31
  • I'm guessing that you're editing someone else's code, right? That's a normal controller `create` action that you're editing. That action will be called whenever a client (or you) visits the route that is associated with it. Run `rake routes` from a terminal window to see what `URI Pattern` is associated with the `Controller#Action` `feed_lists#create`. If you visit that page and create a new `FeedList` using the form there, it will run your code. – Jason Nov 28 '15 at 21:35
  • It's code that I generated with `rails g scaffold`. It's a group project that we're working on - we have other functional `create` actions - I guess I just didn't fully understand (or still don't understand) exactly how the controller functions – Vincent Rodomista Nov 28 '15 at 21:37
  • http://guides.rubyonrails.org/action_controller_overview.html#what-does-a-controller-do-questionmark – Jason Nov 28 '15 at 21:40

1 Answers1

2

Try using a callback in your FeedList model file:

after_create :add_patients

  private
    def add_patients
      Patient.all.each do |p|
        self.patients << p
      end
    end

As Jason mentioned above "create action [that is in your controller] isn't a callback that gets invoked every time you create an instance of a model". See What is a callback function? for information on what is a callback. The after_create callback essentially says to invoke add_patients right after you call FeedList.create

Community
  • 1
  • 1
Kent Shikama
  • 3,910
  • 3
  • 22
  • 55
  • thank you, i'll try that now. Can you explain to a novice what a "callback" is and why you have to put`after_create :add_patients` at the top? – Vincent Rodomista Nov 28 '15 at 21:35
  • 1
    A callback is a generic programming term for a function that runs when your code finishes doing something, like reading a file, opening a video stream, or creating a database record. – Jason Nov 28 '15 at 21:39
  • Now I'm getting `NameError: uninitialized constant FeedList::Patients` How do I initialize it within the FeedList model? – Vincent Rodomista Nov 28 '15 at 21:44
  • Hmm what does your FeedList model class look like? A quick fix might be `::Patients.all.each` but I would need to see the file. – Kent Shikama Nov 28 '15 at 21:47
  • Indent the `after_create` to the same level as `private`. But I don't see any issues there. What is the name of the file for your Patients model along with your FeedList? – Kent Shikama Nov 28 '15 at 22:03
  • its just `patient.rb` – Vincent Rodomista Nov 28 '15 at 22:05
  • Try `Patient` instead of `Patients`. – Jason Nov 28 '15 at 22:06
  • Lol my bad there :p Nice catch @Jason – Kent Shikama Nov 28 '15 at 22:06
  • Until he gave the file name, I assumed he had named his model `Patients`. – Jason Nov 28 '15 at 22:07
  • ok! out of the fire into the frying pan.. `ActiveModel::MissingAttributeError: can't write unknown attribute "feed_list_id"` – Vincent Rodomista Nov 28 '15 at 22:13
  • 1
    You might want to open a new question. You would probably want to give which line the error comes from and the surrounding lines. It also might be worth taking a look at http://api.rubyonrails.org/classes/ActiveModel/MissingAttributeError.html – Kent Shikama Nov 28 '15 at 22:16
  • very big thanks for all the help: problem was solved with a simple `db:migrate` by adding `feed_list_id` to `patient`. It seems to me that using a model for feed list is hardly best practice but it will work! – Vincent Rodomista Nov 28 '15 at 22:36