I would recommend you to remove this code in your view to a presenter and use AR include method to add a your relationships in the same query to fetch from the database. It will avoid de n+1 issue.
2015-05-18 13:23 GMT-03:00 Elizabeth McGurty <emcgur...@gmail.com>: > displaying data > > > > On Monday, May 18, 2015 at 12:21:54 PM UTC-4, Elizabeth McGurty wrote: >> >> Greetings All, >> >> I hope you all understand what I am writing here. Lots of views involve >> displaying date from static 'look up' tables: states, countries, type... >> >> From all of the documentation I have read, the standard code conduct >> seems @look_up_object = LookUpModel.find .where. conditions => ids match >> >> I pursued that structure, and for long listings, so many calls to >> database really bothered me. >> >> So I re-wrote the code on look-up for my case static item_conditions, >> containing only two field: conditions and item_conditions_id: >> >> In the view I now have, following found parent records: >> >> <% @icond = Itemconditions.find(:all, :readonly) %> >> <% @ih = Hash.new %> >> <% @ih2 = Hash.new %> >> <% @ih = @icond.to_a.map(&:serializable_hash) %> >> <% for kv in @ih %> >> <% @ih2[kv.fetch("item_conditions_id")] = >> kv.fetch("conditions") %> >> <% end %> >> >> And then later for iterative record output I have in the view, ldr as the >> parent data source >> >> <label class="item_listing">Condition: <%= >> @ih2[ldr.item_condition_id.to_s] %></label> >> >> Benchmarked this and vastly faster. Deployed it, no problems/errors. I >> have researched Ruby and Ruby on Rails API. I have not found a simpler, >> more native approach. Masters if you are aware of Ruby on Rails way, >> please let me know. >> >> Thanks, >> >> Liz McGurty >> >> >> >> >> >> >> >> >> >> >> >> >> > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscr...@googlegroups.com. > To post to this group, send email to rubyonrails-talk@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/daef460b-110c-4362-a2df-ef8e43aedf6c%40googlegroups.com > <https://groups.google.com/d/msgid/rubyonrails-talk/daef460b-110c-4362-a2df-ef8e43aedf6c%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- thiagocifani http://about.me/thiagocifani <http://del.icio.us/thiagocifani> <http://del.icio.us/thiagocifani> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAGNtUE8BNK%3Dif%3DLXnCj%3DJ9HwGa9ESUNTR8Hp55_v4-b-ca490w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.