I'm upgrading my application from rails 3.2 to rails 4.2.5.
In one of my model i have attr_accessor :user_id and i have defined a getter and setter methods, as i need to value on conditional basic.
class DummyModel < ActiveRecord::Base
attr_accessor :user_id
belongs_to :current_user
before_save :set_user_id
def user_id
self[:user_id] ? self[:user_id] : current_user_id ? current_user.id : nil
end
def user_id=(value)
self[:user_id] = value ? value : current_user_id ? current_user.id : nil
end
private
def set_user_id
self.current_user_id = CurrentUser.first_or_create(:user_id => self[:user_id]).id
end
end
In the above DummyModel, that table doesn't have user_id. But a referance current_user_id which needs to be update by the params user_id which i get from browser.
So when i do DummyModel.new(params[:dummy_model]), it will try to assign the params[:dummy_model][:user_id] to the attr_accessor (With the setter method = user_id=(value)). Once assigned it can be used in the before_save.
This whole thing was working properly in rails 3.2. But when i tried to update to rails 4.2, im getting this error message, as assigning value for attr_accessor with self[:user_id] syntax is removed.
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
So i have one solution to rename the user_id to something else, but user_id is used is many places of my application its not possible to change.
Hope this explains my problem. Thanks for the help in advance.