Model belongs_to

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”?

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. [email protected]