I’m having trouble using update_attributes with referenced documents. I’ve reduced my problem to a simple example that AFAICT should work, but doesn’t:
class Account
include Mongoid::Document
has_many :submissions, :autosave => true
end
class Submission
include Mongoid::Document
belongs_to :account
end
a = Account.new
a.save!
s = Submission.new
s.update_attributes({"account" => {"id" => a.id}})
s.save!
a.id == s.account.id # false
The call to update_attributes is creating a new blank Account object instead of referencing the existing one that I’m telling it to use. What’s going on?
UPDATE
To be clear, I’m trying to process an HTML form in an update action which adds an Account to a Submission. I understand there are other ways to link these documents by writing specific code. But the normal rails way should allow me to use an HTML form to update the documents this way, right?
Change your HTML form to make “account_id” not “account[id]” then it starts working:
Very odd what it’s doing. Maybe mongoid bug?