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?