I have 2 models, User
and Queue
. And I want to be able to add the @user.id
to the queue table from within the users_controller
.
My models are:
class Queue < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
belongs_to :queue
end
and my function in users_controller
is:
def addToQueue
queue_entry = Queue.new(user_id: @user.id) # where things go wrong
if queue_entry.save
redirect_to user_path(@user), notice: "#{@user.name} has been successfully added to the queue."
else
redirect_to user_path(@user), :flash => {:error => "Error: #{@user.name} was NOT added to the queue."}
end
end
I have a link for this function in the show.html.erb
view for a user
as: <%= link_to 'Add to queue', addToQueue_path(@user) %>
For some reason I keep getting: wrong number of arguments (1 for 0)
pointing to the line queue_entry = Queue.new(user_id: @user.id)
How should I be doing this?
The way you set your relationship means that the users table has a foreign key queue_id
and not the other way around.
Since an instance of a User
belongs_to
a Queue
instance then queue_id
has to be assigned as an attribute on the User
instance.