I’m using Rails 3 and mongoid.
I want to save, or update, a value in one record based off of a value in another record.
For example:
class User
include Mongoid::Document
field :account_level, :type => Integer
end
class Profile
include Mongoid::Document
field :position, :type => Integer
end
If the account_level in User is 1 then position in Profile should be updated to 1 also. If account_level is 2, thenposition` should be 2.
This should be done programmatically without input from the user or using an invisible input field on the client (there must be a better way to do it than that).
Extra question…what is I had the following logic (for position), how would I do this?
account_level 1 = position 1
account_level 2 = position 1
account_level 3 = position 5
Not sure if I understood your question.
Try with callbacks ( http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html ):
For example, in User class: