Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#3042

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.

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

State: invalid
Milestone: none
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. Morgan Christiansson
    Morgan Christiansson

    (Sorry about the formatting in the first 2 code snippets, they were from script/console which has a prefix of >> for all commands which has been interpreted as quotation.)

    My workaround did not work as it does not import the instance methods, my new workaround is to rename my Event model to Event2 to avoid the naming conflict. This in turn has caused problems with associations and fixtures - not happy. :(

    August 12th, 2009 @ 06:07 PM

  2. Tekin
    Tekin

    I'm having the same problem.

    It seems the call to active_record.compute_type in AssociationReflection#klass is getting itself in a dither and returning the wrong constant. I'll write a failing test when it's not half five on a Friday afternoon and my brain is in a better state (hoho).

    August 21st, 2009 @ 04:40 PM

  3. Tekin
    Tekin

    Having trouble recreating this in rails for a testcase. I have a workaround though, simply pass in :class_name => '::Event' to force compute_type to resolve to the correct Event class.

    August 24th, 2009 @ 12:25 PM

  4. Steve St. Martin
    Steve St. Martin
    • Assigned user set to Ryan Bigg

    ActiveModel::StateMachine was removed from core, should be marked as invalid

    April 16th, 2010 @ 01:55 AM

  5. Ryan Bigg
    Ryan Bigg
    • State changed from new to invalid

    April 19th, 2010 @ 10:12 PM

  6. a4490302sc
    a4490302sc
    • Importance changed from to

    nshiny sapphire designer replica Piaget external. What more could replica Best Sellers wholesale you conceivably lust or Jacob & Co. Unisex Watch JC1 sale relish when buying your wholesale replica Emporio Armani manipulate from Omega, illustrate replica handbags a no mishap additional designer replica Panerai additional see. represent the replica Ugg New Style Boots for sale say so of high-society. replica Plastic Sunglasses online To pass on cheap replica jewelry in consummate your personality, replica Puma Shoes online store you should pour in a Jimmy Choo handbag.Our website off

    July 21st, 2010 @ 02:37 AM