This is EXTREMELY bizarre. I’m upgrading a Rails 2.3.12 app and running into this same problem over and over again. I’m stumped and nothing else out there seems to touch on it.
I have two models:
class User < ActiveRecord::Base
has_many :logs, :class_name => 'UserLog'
end
and
class UserLog < ActiveRecord::Base
attr_accessor :site_id, :controller, :action, :url, :session
belongs_to :user
validates_presence_of :user
end
then in another controller I’m doing this:
def log_user_activity
@current_user.logs.create(:site_id => @site.id, :controller => params[:controller],
:action => params[:action], :url => request.path,
:session => request.session_options[:id]) if @current_user
end
as you can see, it’s pretty straightforward but when I call log_user_activity I’m getting this:
Can't mass-assign protected attributes: site_id, controller, action, url, session
HOWEVER, if I change all my creates or builds to this:
def log_user_activity
log = @current_user.logs.new
log.site_id = @site.id
log.controller = params[:controller]
log.action = params[:action]
log.url = request.path
log.session = request.session_options[:id]
log.save
end
then it works fine!?
Has anyone seen this? Any clues?
In class UserLog, add the following:
The reason you have to use
attr_accessibleis most likely because you are utilizing a plugin that is relying on this being present for a model. It has happened to all of us and is a royal pita)Once
attr_accessibleis designated for a class, then any attribute that is not specified as ‘accessible’ will not be allowed to be updated.