On Jueves, 28 de Julio de 2011 23:10:31 Alan escribió:
> Here is a simplified snippet of code that I have:
> ===========================================================================
> == class Approval < ActiveRecord::Base
>       belongs_to :user
>       belongs_to :event
> end
> ---------------------------------------------------------------------------
> -- class Admin::ApprovalsController < Admin::AdminController
>       active_scaffold do |config|
>               config.columns = [:user, :event, :status, :created_at]
>       end
> end
> ---------------------------------------------------------------------------
> -- module Admin::ApprovalsHelper
> end
> ---------------------------------------------------------------------------
> -- class User < ActiveRecord::Base
>       def to_label
>               "test"
>       end
> end
> ===========================================================================
> ==
> 
> By default, this will execute the following SQL:
>   ←[1m←[36mSQL (0.0ms)←[0m  ←[1mSELECT COUNT(*) FROM `approvals`←[0m
>   ←[1m←[35mApproval Load (1.0ms)←[0m  SELECT `approvals`.* FROM
> `approvals` ORDER BY `approvals`.`id` ASC LIMIT 15 OFFSET 0
>   ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT `users`.* FROM `users`
> WHERE (`users`.`id` IN (106,9,2820,1,2,3351,500))←[0m
>   ←[1m←[35mEvent Load (1.0ms)←[0m  SELECT `events`.* FROM `events`
> WHERE (`events`.`id` IN (24,25,26,15,1))
> 
> Is it possible to optimize this, so that it only loads the columns
> that I specify? and so that it does it in a single query w/ joins?
> 

You won't get better performance getting all associations in a query. It was 
like that in previous rails versions and it was changed becuase it was better 
to get associations in different queries.

You can force rails to load associations in the same query setting conditions 
about the association, but then rails doesn't allow you to set select in the 
query.

However, you are right it's better to get only needed columns for 
associations, but rails doesn't have a way to do it, it's a ActiveRecord 
limitation.

The best you can do is using virtual columns, as you said, include user_id 
instead of user and override column with a helper to display a link.

I have found eload-select plugin, but it's old and unmaintained, only for 
rails 2.3

-- 
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) [email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en.

Reply via email to