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.

3.0.5 AR: has_many :through - wrong table name

#6600

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

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

State: duplicate
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Andrew White
    Andrew White
    • Importance changed from to Low

    Duplicate of #6479 Model associations are trying to select singular table names after 3.0.5 upgrade.

    Strictly speaking criteria is the plural of criterion - your class name should be RecordCriterion. However this still doesn't work so you'll need to add a custom inflection to your inflections.rb initializer.

    March 21st, 2011 @ 11:56 AM

  2. Andrew White
    Andrew White
    • State changed from new to duplicate

    March 21st, 2011 @ 11:56 AM