This project is archived and is in readonly mode.

#3042 ✓invalid
Morgan Christiansson

include ActiveModel::StateMachine imports classes defined in ActiveModel::StateMachine module

Reported by Morgan Christiansson | August 12th, 2009 @ 03:01 PM

I have a model called Event defined.


class Booking < ActiveRecord::Base
belongs_to :event end

class Event < ActiveRecord::Base
end

When i add include ActiveModel::StateMachine to my Booking model the association stops working.


Booking.last.event NameError: undefined local variable or method event' for #<Class:0x7f68edac8728><br/>

from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/base.rb:1959:in `method_missing'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/base.rb:2199:in `compute_type'
from #{RAILS_ROOT}/vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/base.rb:2195:in `compute_type'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/reflection.rb:156:in `send'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/reflection.rb:156:in `klass'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb:49:in `find_target'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:240:in `load_target'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:112:in `reload'
from #{RAILS_ROOT}/vendor/rails/activerecord/lib/active_record/associations.rb:1247:in `event'
from (irb):2

I tracked the problem to ActiveModel::StateMachine::Event being imported as can be seen from this code:


Booking.reflections[:event] => #<ActiveRecord::Reflection::AssociationReflection:0x7fa1123db200 @klass=ActiveModel::StateMachine::Event, @primary_key_name="event_id", @active_record=Booking(id: integer, event_id: integer), @options={}, @macro=:belongs_to, @class_name="Event", @name=:event>

So .find is being called on ActiveModel::StateMachine::Event.

If i instead use:

require 'active_model/state_machine/machine' extend ActiveModel::StateMachine::ClassMethods

Then the behaviour is correct.


?> Booking.reflections[:event] => #<ActiveRecord::Reflection::AssociationReflection:0x7f3505b2a840 @klass=Event(id: integer, name: string), @primary_key_name="event_id", @active_record=Booking(id: integer, event_id: integer), @options={:class_name=>"Event"}, @macro=:belongs_to, @class_name="Event", @name=:event>

This is very different from the expected behaviour where the Event constant is not redefined and association works as normal.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages