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

Problem with initial_state and mongoid #68

Merged
merged 1 commit into from
Jan 19, 2013

Conversation

simonc
Copy link

@simonc simonc commented Jan 18, 2013

Using mongoid (v3.0.17) I have an issue with the initial state setting.

product = Product.new
product.current_state #=> :available
product.valid? #=> false
product.errors #=> ... {:state=>["can't be blank", "is not included in the list"]}

ActiveModel#Transitions.set_initial_state tests for has_attribute?(:state).

self.state ||= self.class.get_state_machine.initial_state.to_s if self.has_attribute?(:state)

Mongoid returns false if the field is not set. Maybe it should only check respond_to?(:state=) don't you think ?

ActiveModel::Transitions#set_initial_state was not working with mongoid.
It was check for has_attribute?(:state) which returns false if the field
has no value in a mongoid document.

Using respond_to?(:state=) fixes the issue and keeps the behavior.
troessner added a commit that referenced this pull request Jan 19, 2013
Problem with initial_state and mongoid
@troessner troessner merged commit 9dab33a into troessner:master Jan 19, 2013
@troessner
Copy link
Owner

Excellent, thanks!

@troessner
Copy link
Owner

@simonc I'm afraid that commit broke something in transitions: #76
Do you think you could look into this and come up with another solution?
I'm afraid I can't do this myself since I know exactly ZERO about mongoid.
Otherwise I'm afraid I'll have to revert that commit since I value the use case in the referenced issue a little higher than mongoid support.

@troessner
Copy link
Owner

I'm sorry, but I had to revert this pull request - let me know if you come up with something that fixes mongoid support again without breaking other functionality and I'll be more than happy to merge that again..:)

@simonc
Copy link
Author

simonc commented Mar 1, 2013

No problem. I'm sorry, I didn't have time to take a new look at the issue :/
I'll see what I can do but I have a lot on my plate right now :)

@simonc simonc deleted the 68-mongoid-initial-state branch March 1, 2013 09:47
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

Successfully merging this pull request may close these issues.

2 participants