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

'Event' naming collision #123

Closed
alexhanh opened this issue Mar 15, 2014 · 4 comments
Closed

'Event' naming collision #123

alexhanh opened this issue Mar 15, 2014 · 4 comments

Comments

@alexhanh
Copy link
Contributor

I have an Event model in my Rails project and I do this:

  def event_fired(current_state, new_state, event)
    Event.create!(type: "s.#{event}", transfer: self)
  end

with this, it throws undefined method 'create!' for Transitions::Event:Class. It seems it tries to use Transitions::Event instead of my Event model.

Is there any clean way to fix this?

@alexhanh
Copy link
Contributor Author

I found two ways so far:

  1. Define the relationship before including Transitions
  has_many :events
  include ActiveModel::Transitions
  1. Use a namespace for the model

Would love to hear your input.

@troessner
Copy link
Owner

  1. Define the relationship before including Transitions

I think that's a bad hack like everything that relies on ordering. But you probably agree with that already.:)

  1. Use a namespace for the model

I think that's better but still not optimal.

You can do:

  def event_fired(current_state, new_state, event)
    ::Event.create!(type: "s.#{event}", transfer: self)
  end

I just tested it and this works for me. Also, I think this is the ruby-idiomatic way of solving this problem.

Let me know if this works for you and what you think of this solution, then I'll update the README to make this easier for others in the future.

@alexhanh
Copy link
Contributor Author

Ahh, I didn't know you could refer to the top-level Event object in the namespace in that way, that's neat! Thanks for the help!

@troessner
Copy link
Owner

Addendum: I updated the README to make this clearer to everybody: 883dc00

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

2 participants