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?

7
  • That looks fine to me. Commented Nov 28, 2015 at 21:26
  • pls see edit, it doesnt seem to be working @jason Commented Nov 28, 2015 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? Commented Nov 28, 2015 at 21:29
  • it's in the feed_lists_controller.rb file Commented Nov 28, 2015 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. Commented Nov 28, 2015 at 21:35

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

12 Comments

thank you, i'll try that now. Can you explain to a novice what a "callback" is and why you have to putafter_create :add_patients at the top?
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.
Now I'm getting NameError: uninitialized constant FeedList::Patients How do I initialize it within the FeedList model?
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.
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 api.rubyonrails.org/classes/ActiveModel/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.