We have an ActiveRecord model whose columns have some default values. It also has a validation condition such that, if the 'profile' property is set to a different value, then the default values are invalid.
What I'd like is to be able to determine whether the attributes have been set since they were set to the default so that I can reset them to new, valid values before validation.
Is this possible?
UPDATE: It's not clear what I mean, so I should give code examples.
The table is defined like this:
t.column :profile, :string
t.column :contact_by, :string, :default => "phone", :null => false
The validation is like this:
validate :validate_contact_by_for_profile1
def validate_contact_by
if (profile == "profile1") && (contact_by != "email")
errors.add(:contact_by, " must be 'email' for users in profile1")
end
end
So any time we do this:
u = User.new
u.profile => profile1
We end up with u
being invalid. What I want to end up with is that the user's contact_by defaults to "phone", but if their profile is set to profile1, then it changes to "email", unless it has been set to something in the meantime. (Ideally this includes setting it to "phone")