Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoid (v3) field aliases keep state from being saved #270

Open
xentek opened this issue Jul 12, 2013 · 0 comments
Open

Mongoid (v3) field aliases keep state from being saved #270

xentek opened this issue Jul 12, 2013 · 0 comments

Comments

@xentek
Copy link

xentek commented Jul 12, 2013

We defined our Mongoid field with an alias. Here is a simple example class to illustrate the issue:

class MyModel
  include Mongoid::Document

  field :st, as: :status, type: String

  state_machine :status, initial: :draft do
    event :publish do
      transition :draft => :published
    end
  end
end

The issue is exhibited when you have an instance of MyMongoid and try to call publish! on it. The status field is changed to published, but the model is not saved. If you try to save it manually (with save or save!), the status is changed back to draft when you reload or re-query the object from the database.

Fixing it was easy, we just renamed the field to :status and left the alias out. Not ideal, but did take a lot of debugging to figure out what was causing the behaviour explained above. I also reason that we could have just changed the state machine to use the real field name (e.g. state_machine :st ...) and it would have worked, though that would muddy up our domain and hide our intentions behind the abbreviation.

If I get some free time soon, I'll try to work up a patch, but right now I have people to see, fat to chew, and a feature to ship. :shipit:

More info on the field aliasing feature: http://mongoid.org/en/mongoid/docs/documents.html#field_aliasing (you may need to scroll up a little, the hash anchor was a little screwy for me)

Cheers, and thanks for an otherwise super solid library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant