weird behavior of belongs_to referencing a model with set_table_name : a bug?

Hello all,

Since nobody answered the letter below in rubyonrails-talk, I’ve taken the courage to repost it here.

I looks like a bug, but I just refuse to believe it as it is such a basic functionality of Active Record.

Hello all,

Since nobody answered the letter below in rubyonrails-talk, I've taken the courage to repost it here. I looks like a bug, but I just refuse to believe it as it is such a basic functionality of Active Record.

It does seem strange. Can you reproduce the bug in the active record tests? If so you can open a trac ticket and we can take a look at it.

Hi, I think I have a related problem (that I was about to post)

...

I have the following problem With a legacy Database:

class User < ActiveRecord::Base   set_table_name :jos_users

  has_many :orders   has_many :order_items, :through => :orders end

class Order < ActiveRecord::Base   set_table_name :jos_vm_orders   set_primary_key :order_id

  has_many :order_items   belongs_to :user end

class OrderItem < ActiveRecord::Base   set_table_name :jos_vm_order_item   set_primary_key :order_item_id

  belongs_to :order end

When I try to get all order items for a specific user I get an error:

u=User.find(63).order_items

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'jos_vm_orders.order_item_id' in 'on clause': SELECT jos_vm_order_item.* FROM jos_vm_order_item INNER JOIN jos_vm_orders ON jos_vm_order_item.order_id = jos_vm_orders.order_item_id WHERE ((jos_vm_orders.user_id = 63))   from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:150:in `log'

I don't understand why rails is trying to get jos_vm_orders.order_item_id instead of jos_vm_orders.order_id, since the primary key of orders table os order_id.

Can somebody help.

Tahnks, Cláudio Caseiro

Hi, I think I have a related problem (that I was about to post)

...

I have the following problem With a legacy Database:

class User < ActiveRecord::Base   set_table_name :jos_users

  has_many :orders

You need to add the option :foreign_key => "order_id"

so    has_many :orders, :foreign_key => "order_id"

With that foreign_key I still have the problem, but I don't think I need any foreign_key. I'm in class User, so if I need any foreign_key in a has_many association it would be :user_id, but since that is the default I don't need any.

I think the problem is that the join is using the primary_key defined in the OrderItem class for the order table, what is not correct. I changed that to "set_primary_key :foo" and I get

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'jos_vm_orders.foo' in 'on clause': SELECT `jos_vm_order_item`.* FROM `jos_vm_order_item` INNER JOIN jos_vm_orders ON jos_vm_order_item.order_id = jos_vm_orders.foo WHERE ((`jos_vm_orders`.user_id = 63))

Any help? Thanks, Claudio Caseiro

Rick Denatale wrote:

Can you test whether you have the same problems if you change your database to use the standard Rails naming scheme, i.e. so that you don't have to use any of the table_name, set_primary_key etc. methods? I'm not suggesting that you'd have to do it in your actual product, but it could help figuring out whether some of them is breaking the scheme.

//jarkko