I’m currently reading Rails 3 In Action. The book creates an app where one can create Projects and for each Project on can create Tickets. It creates 3 models:
Project:
class Project < ActiveRecord::Base
attr_accessible :name
validates :name, presence: true
has_many :tickets, :dependent => :destroy
end
Ticket:
class Ticket < ActiveRecord::Base
belongs_to :project
belongs_to :user
attr_accessible :description, :title
validates :title, presence: true
validates :description, presence: true, :length => { :minimum => 10 }
end
and User:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Now when I add the following line to the create action inside the tickets_controller:
@ticket = @project.tickets.build(params[:ticket].merge!(:user => current_user))
I get this error Can't mass-assign protected attributes: user. Now I don’t really understand what merge! is doing and why :user is being passed in or why I’m getting the error. I know that normally I would have to include the mass-assignment attribute to the attr_accessible: method. But this time the attribute is a class so I don’t know how to handle this.
help,
mike
Unless there’s a reason why you need this on a single line? In which case maybe