# Weird behavior of belongs\_to referencing a model with set\_table\_name

**URL:** <https://www.ruby-forum.com/t/weird-behavior-of-belongs-to-referencing-a-model-with-set-table-name/128156>\
**Category:** Rails\
**Created:** [February 5, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/weird-behavior-of-belongs-to-referencing-a-model-with-set-table-name/128156 "2008-02-05T13:25:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuleven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@yuleven](https://www.ruby-forum.com/u/yuleven)\
**Post date:** [February 5, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/weird-behavior-of-belongs-to-referencing-a-model-with-set-table-name/128156/1 "2008-02-05T13:25:04Z")

</div>

Hello all,  
I am experiencing some really weird behavior of belongs\_to with a model  
where the table name is set by set\_table\_name

I have two simple models:

=== file legal\_entity.rb

class LegalEntity \< ActiveRecord::Base  
set\_table\_name ‘legalentities’  
end

=== file legal\_entity\_person.rb

class LegalEntityPerson \< ActiveRecord::Base

set\_table\_name ‘legalentities\_people’

belongs\_to :legal\_entity, :foreign\_key =\> ’ legalentity\_id’  
end

=====

If I find a LegalEntityPerson object with :include =\> :legal\_entity it  
works  
as expected:

$ ./script/runner ‘a = LegalEntityPerson.find(:first, :include =\>  
:legal\_entity); p a.legal\_entity’  
#\<LegalEntity id: 1, name: “Some Company”, legalentitytype\_id: 3\>

and looking a the log I see the select method with the 2 tables joined,  
it’s  
fine.

Now, if I drop :include =\> :legal\_entity, a.legal\_entity returns nil:

$ ./script/runner ‘a = LegalEntityPerson.find(:first); p  
a.legal\_entity’  
nil

And what is interesting is that there is no SQL query to the DB, that  
is,  
there is SELECT \* FROM `legalentities_people` LIMIT 1 for the find  
method,  
and nothing else, a.legal\_entity silently returns nil without any  
warnings  
or complaints.

If I rewrite my models like this following naming conventions:

=== file legalentity.rb

class Legalentity \< ActiveRecord::Base

end

=== file legal\_entity\_person.rb

class LegalEntityPerson \< ActiveRecord::Base

set\_table\_name ‘legalentities\_people’

belongs\_to :legalentity  
end

=====

it works:

$ ./script/runner ‘a = LegalEntityPerson.find(:first); p a.legalentity’  
#\<Legalentity id: 1, name: “Some Company”, legalentitytype\_id: 3\>

BUT!!!

If I specify :foreign\_key explicitly like this:

belongs\_to :legalentity, :foreign\_key =\> ’ legalentity\_id’

it doesn’t work again:

Is there any reason behind this behavior or is it a bug?

I am using Rails 2.0.2, mysql 5.0.51a, all running on OS X, and with  
the  
following sql code you can reproduce it:

CREATE TABLE `legalentities` (  
`id` int(10) unsigned NOT NULL auto\_increment,  
`name` varchar(255) NOT NULL,  
`legalentitytype_id` int(10) unsigned NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB AUTO\_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `legalentities` VALUES (1,‘Some Company’,3);

CREATE TABLE `legalentities_people` (  
`id` int(10) unsigned NOT NULL auto\_increment,  
`legalentity_id` int(10) unsigned NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB AUTO\_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `legalentities_people` VALUES (1,1);

–  
Best regards,  
Yuri L.
