I have a rake task to create a number of invoices
in my Rails application:
def make_invoices
Project.all.each do |project|
i = project.invoices.create!( :number => random_number,
:date => random_date,
:recipient => random_recipient,
:user_id => project.person.user.id)
i.created_at = random_time
i.save
end
end
This line causes the rake task to fail, however:
:user_id => project.person.user.id
And I get the following error:
Can't mass-assign protected attributes: user_id
I know why this happens.
But I need to run this rake task and I wonder if there's any way to force mass-assign or something?
Each invoice's project_id
is set automatically by the invoices.create!
method. But what if I need to set a user_id
as well?
Thanks for any help.