This project is archived and is in readonly mode.

#6600 ✓duplicate
Strelok

3.0.5 AR: has_many :through - wrong table name

Reported by Strelok | March 21st, 2011 @ 09:24 AM

I'm using rails 3.0.5

I have Model Card

class Card < ActiveRecord::Base
  has_many :records, :foreign_key => 'card_id', :dependent => :destroy
end

Model HospitalCard

class HospitalCard < Card
  has_one :last_record, :class_name => 'Record', :foreign_key => 'card_id', :order => 'id DESC'
  has_many :record_criterias, :through => :last_record
end

Model Record

class Record < ActiveRecord::Base
  belongs_to :card
end

And

class RecordCriteria < ActiveRecord::Base
  belongs_to :record
end

When I try

Card.find(1).record_criterias

ActiveRecord::StatementInvalid: ODBC::Error: S0002 (208) [Microsoft][SQL Native Client][SQL Server]Invalid object name 'record_criteria'.: SELECT [record_criteria].* FROM [record_criteria] INNER JOIN
[records] ON [record_criteria].record_id = [records].id WHERE [record_criteria].[deleted_at] IS NULL AND (([records].card_id = 1) AND (((records.deleted_at IS NULL))))
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `log'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-3.0.10/lib/active_record/connection_adapters/sqlserver/database_statements.rb:249:in `raw_select'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-3.0.10/lib/active_record/connection_adapters/sqlserver/database_statements.rb:193:in `select'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/base.rb:467:in `find_by_sql'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/relation.rb:64:in `to_a'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/relation/finder_methods.rb:143:in `all'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/relation/finder_methods.rb:105:in `send'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/relation/finder_methods.rb:105:in `find'
        from C:0:in `__send__'
        from C:0:in `find'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/associations/has_many_through_association.rb:84:in `find_target'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/base.rb:1121:in `with_scope'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:203:in `send'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:203:in `with_scope'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/associations/has_many_through_association.rb:84:in `find_target'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/associations/association_collection.rb:410:in `load_target'
        from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:145:in `inspect'
        from C:/Ruby/lib/ruby/1.8/irb.rb:310:in `output_value'
        from C:/Ruby/lib/ruby/1.8/irb.rb:159:in `eval_input'
        from C:/Ruby/lib/ruby/1.8/irb.rb:271:in `signal_status'
        from C:/Ruby/lib/ruby/1.8/irb.rb:155:in `eval_input'
        from C:/Ruby/lib/ruby/1.8/irb.rb:154:in `eval_input'
        from C:/Ruby/lib/ruby/1.8/irb.rb:71:in `start'
        from C:/Ruby/lib/ruby/1.8/irb.rb:70:in `catch'
        from C:/Ruby/lib/ruby/1.8/irb.rb:70:in `start'
        from C:/Ruby/lib/ruby/gems/1.8/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
        from C:/Ruby/lib/ruby/gems/1.8/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby/lib/ruby/gems/1.8/gems/railties-3.0.5/lib/rails/commands.rb:23
        from script/rails:6:in `require'
        from script/rails:6irb(main):017:0> 1

'record_criteria' - wrong table name I have 'record_criterias'

Not depends on database adapter. I tested it via sqlite3 and sqlserver with the same result.

In 3.0.4 all works properly

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