# Model belongs\_to

**URL:** <https://www.ruby-forum.com/t/model-belongs-to/162778>\
**Category:** Rails\
**Created:** [March 13, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/model-belongs-to/162778 "2009-03-13T17:35:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 13, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/model-belongs-to/162778/1 "2009-03-13T17:35:38Z")

</div>

If I have in Order -model row: belongs\_to :customer it joins two tables  
and show customers information. But if I don’t need user informatioan,  
can I temporarily disable it and speed up “select”?

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 13, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/model-belongs-to/162778/2 "2009-03-13T17:58:19Z")

</div>

short answer is no. for several reasons:

1. Rails find methods select all columns by default, and by taking the  
belongs\_to out of the class (which is how i assume u want to “diasble”  
it) it will still include the attribute customer\_id it in the results.

2. Rails doesnt load associated objects by default unless you  
explicitely tell it to with the :include switch so:

order = Order.first =\> will give you a new Order(customer\_id, …)  
but the customer it is assocuiated with will not be there! just the  
reference (id:integer). if i then go order.customer, you will see  
another select in the logs

Order.first(:include =\> :customer) will load the customer as well. so  
if i then go order.customer, the db is not touched again because i  
already loaded it.

If you want to speed thing up (a very tiny little bit) check out what  
methods you are calling in order and only select the attributes you  
will use. so for example, if you are only marking it complete you can  
do something like:

order = Order.first(:select =\> :completed)  
order.update\_attributes :completed =\> true  
order.save

beware that if you (by mistake) need a attribute you have not loaded  
rails will raise an AttributeNotFound (i think is the name) exception

as a sidenote, it is wise not to perform optimisations untill you  
profile your program as a whole. so as a rule of thumb, od it the  
simple stupid way first, then gather stats of how your software is  
ebing used and then optimise on the most comonly used functions. If  
not, you might end up spending hours optimising a function that is  
only called once or twice a month.

On Mar 13, 5:35 pm, Fresh M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
