I have the following model:
class Question < ActiveRecord::Base
belongs_to :user, :readonly => true
end
I would expect that the :readonly => true attribute would prevent the user from being changed i.e.
# assume we've set up user_1, user_2 and question
# and that question.user == user_1
question.user = user_2
question.save
question.reload
question.user == user_2 # true - why?
Why is this true – I expected that :readonly=> true would prevent this attribute from being changed after initial creation? If it doesn’t then what does the :readonly option actually do?
EDIT
Using attr_readonly will give the non-changeability (immutability?) that I was looking for.
class Question < ActiveRecord::Base
belongs_to :user
attr_readonly :user_id
end
The only problem with it is that it never raises an exception when you try to change the attribute so it’s easy to get bugs out of silent fails.
From the doc,
So I suppose that prevents you from doing things such as
But let you continue to modify the relation itself.