i have a model with 35000 entries in the database. I was using a unique string as the primary key. Right now I am using the order method to return the model objects in order. I''d like to know what is good practice ( efficient ) and how to return X entries at a time instead of all at once. Right now I''m using the index method of a controller along with the order method to return the entries, and then iterating through them in the view. class StoreController < ApplicationController def index @dicts = Dict.order(:key) end end <h1>Store#index</h1> <p>Find me in app/views/store/index.html.erb</p> <% @dicts.each do |word| %> <div class=''entry''> <%= word.key %> </div> <%end %> I''d also like to know a little more about how conrtrollers work with the database. Is a new store controller instanced everytime a route is directed to it? If so am I continually querying the database for those 35000 entries, or is one instance of the controller generated, and one request made. I have read some of the rails guides, and read quite a bit of agile web dev with rails 4, but haven''t found the answer to my questions. Do you think you know a reference you would recommend I read? -- 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0b1b8461-ca5a-4442-acd4-b32fbfac1bf4%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
You need pagination, visit this [1] [1]: http://railscasts.com/episodes/254-pagination-with-kaminari On Wed, Dec 11, 2013 at 4:29 AM, Colin Williams <ke7cfn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i have a model with 35000 entries in the database. I was using a unique > string as the primary key. Right now I am using the order method to return > the model objects in > order. I''d like to know what is good practice ( efficient ) and how to > return X entries at a time instead of all at once. Right now I''m using the > index method of a controller along with the order method to return the > entries, and then iterating through them in the view. > > class StoreController < ApplicationController > def index > @dicts = Dict.order(:key) > end > end > > <h1>Store#index</h1> > <p>Find me in app/views/store/index.html.erb</p> > <% @dicts.each do |word| %> > <div class=''entry''> > <%= word.key %> > </div> > <%end %> > > > I''d also like to know a little more about how conrtrollers work with the > database. Is a new store controller instanced everytime a route is directed > to it? If so am I continually querying the database for those 35000 > entries, or is one instance of the controller generated, and one request > made. I have read some of the rails guides, and read quite a bit of agile > web dev with rails 4, but haven''t found the answer to my questions. Do you > think you know a reference you would recommend I read? > > -- > 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/0b1b8461-ca5a-4442-acd4-b32fbfac1bf4%40googlegroups.com > . > For more options, visit https://groups.google.com/groups/opt_out. >-- *greatghoul <http://www.g2w.me> - Ask and Learn!* -- 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAFoKNZODPwjmoK%2BKa_CD%3DLYs4FxPQAZG0gPe2HEhAJP%2BHSndVQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Tuesday, December 10, 2013 8:29:17 PM UTC, Colin Williams wrote:> > i have a model with 35000 entries in the database. I was using a unique > string as the primary key. Right now I am using the order method to return > the model objects in > order. I''d like to know what is good practice ( efficient ) and how to > return X entries at a time instead of all at once. Right now I''m using the > index method of a controller along with the order method to return the > entries, and then iterating through them in the view. > >Typically you load one page of results at a time. kaminari and will_paginate are 2 popular ways of doing this. Under the hood they are just setting limit and offset query> > > I''d also like to know a little more about how conrtrollers work with the > database. Is a new store controller instanced everytime a route is directed > to it? If so am I continually querying the database for those 35000 > entries, or is one instance of the controller generated, and one request > made. I have read some of the rails guides, and read quite a bit of agile > web dev with rails 4, but haven''t found the answer to my questions. Do >a new controller is instantiated with each request. so your code hits the database for every single page view (which you can easily prove to yourself by adding a breakpoint or logger statements to the body of your controller method or watching the queries stack up in the log file Fred -- 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3f8e04b8-5f81-4692-a678-60bc197ec9ce%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.