Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5425 (reopened defect)

Opened 2 years ago

Last modified 10 months ago

using STI doesn't work with has_and_belongs_to_many

Reported by: anonymous Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 1.2.3
Severity: major Keywords:
Cc:

Description

Hypothetical class hierarchy:

def Band < ActiveRecord::Base
  has_and_belongs_to_many :fans
end

def RedHotChiliPeppers < Band
end

def Fan < ActiveRecord::Base
  has_and_belongs_to_many :bands
end

fire up the console and create some objects:

>> fan = Fan.new
>> band = RedHotChiliPeppers.new
>> fan.bands
=> []
>> fan.bands<< band
ActiveRecord::AssociationTypeMismatch: Band expected, got RedHotChiliPeppers
        from /usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.1/lib/active_record/associations/association_proxy.rb:134:in `raise_on_type_mismatch'
        from /usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.1/lib/active_record/associations/association_collection.rb:24:in `<<'
        from /usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.1/lib/active_record/associations/association_collection.rb:23:in `<<'
        from /usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.1/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction'
        from /usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.1/lib/active_record/transactions.rb:91:in `transaction'
        from /usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.1/lib/active_record/transactions.rb:118:in `transaction'
        from /usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.1/lib/active_record/associations/association_collection.rb:22:in `<<'
        from (irb):33

Change History

03/11/07 00:35:13 changed by ibroadfo

  • status changed from new to closed.
  • version set to 1.2.2.
  • resolution set to fixed.
>> fan = Fan.new
=> #<Fan:0x2456270 @new_record=true, @attributes={}>
>> band = Chillis.new
=> #<Chillis:0x138d0cc @new_record=true, @attributes={}>
>> fan.bands
=> []
>> fan.bands << band
=> [#<Chillis:0x138d0cc @new_record=true, @attributes={}>]

09/06/07 21:34:54 changed by rosaldanha

  • status changed from closed to reopened.
  • version changed from 1.2.2 to 1.2.3.
  • resolution deleted.

I´m got the same error, with this same case. I´m running 1.2.3