On Viernes, 17 de Septiembre de 2010 15:11:26 Iñaki Baz Castillo escribió: > Hi, :client has_many :numbers. "numbers" table has a lot of columns > I'm not interested in. And some clients has *a lot* of numbers. > > > The problem: > > When I display the ClientsController action "list", AS loads all the > associated numbers (with all their columns) for the displayed clients > (20-40), but finally, the column "Numbers" just shows the total number > of "numbers". Loading so many "numers" entries is a very expensive > operation and takes long time, too much, which makes the whole > application very slow. > > However I've realized that client.numbers.size is a fast operation. So > what I would like is: > > > 1) When displaying "clients" list, for each client just its total > number of "numbers" to be computed, no more.
set includes to nil for numbers column. You will a get a numbers query for each row. If associated_limit is 0, only count query will be issued. You could use cache_count and you should get rid of count queries (I think so, I'm not sure if I got it). > > 2) When I click in a "Numbers" column then I would see the typical > nested view of "client" associated "numbers". If it's possible, just > some specific columns of "numbers" table should be loaded (those that > the NumbersControllers shows in the "list" action"). > Define custom_finder_options in numbers controller to return :select => 'columns list' > In this way I would get *exactly* the same result as the current one, > but in a much more efficient way. > > > The question: > > Is it possible? I've got something "similar" by creating a virtual > column "num_numbers" in Client model: > > def num_numbers > self.numbers.size > end > > And then, in ClientsHelpers I've created a field override to show a > link to Numbers list by filtering numbers belonging to the client: > > def client_num_numbers_column(client) > link_to client.num_numbers, :action => :list, :controller => :numbers > end > > > However this is a not very good solution because: > > - I compute twice client.numbers.size (first time whe AS loads the > data, and second time when AS applies the field override). I could > store client.numbers.size in a Client attribute but it seems a bit > ugly. > > - I don't get a nested view, but a link to other page, which is not so > cool. > That way could be improved, but it isn't needed because you can get it with AS configuration. -- 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.
