In my application, I have a common layout (''/app/views/layouts/layout.rhtml''). In that file, I want to iterate over the 10 most recent NewsPosts to create links for them. I can do that by putting: @recent_news = NewsPost.find(:all, :order => ''created_on DESC'', :limit => 10) in every controller so that @recent_news is accessible to all when Rails goes to render the layout. Of course, that''s not very DRY. I have tried putting that in application.rb and application_helper.rb (and maybe some other places), but the NewsPost model isn''t in scope there. So what''s the proper way to handle this sort of thing? (I apologize if this is often-asked, but I can''t find an answer) Thanks, Myles
Hey Myles, you can define a "before_filter" in your ApplicationController which calls the news list for each request. Second way would be creating a Component (http://manuals.rubyonrails.com/read/book/14) for your recent news. .b Am 30.04.2005 um 19:18 schrieb Myles Grant:> In my application, I have a common layout > (''/app/views/layouts/layout.rhtml''). In that file, I want to iterate > over the 10 most recent NewsPosts to create links for them. I can do > that by putting: > > @recent_news = NewsPost.find(:all, :order => ''created_on DESC'', :limit > => 10) > > in every controller so that @recent_news is accessible to all when > Rails goes to render the layout. Of course, that''s not very DRY. I > have tried putting that in application.rb and application_helper.rb > (and maybe some other places), but the NewsPost model isn''t in scope > there. > > So what''s the proper way to handle this sort of thing? (I apologize > if this is often-asked, but I can''t find an answer) > > Thanks, > Myles > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Julian ''Julik'' Tarkhanov
2005-Apr-30 20:34 UTC
Re: Application-wide model scope question
On 30-apr-05, at 19:18, Myles Grant wrote:> In my application, I have a common layout > (''/app/views/layouts/layout.rhtml''). In that file, I want to iterate > over the 10 most recent NewsPosts to create links for them. I can do > that by putting: > > @recent_news = NewsPost.find(:all, :order => ''created_on DESC'', :limit > => 10) > > in every controller so that @recent_news is accessible to all when > Rails goes to render the layout. Of course, that''s not very DRY. I > have tried putting that in application.rb and application_helper.rb > (and maybe some other places), but the NewsPost model isn''t in scope > there. > > So what''s the proper way to handle this sort of thing? (I apologize > if this is often-asked, but I can''t find an answer) >What do you mean "not in scope"? The instance variable is not accessible from the layout or the model class is not defined in your controller? -- Julian "Julik" Tarkhanov
Julian ''Julik'' Tarkhanov wrote:> > On 30-apr-05, at 19:18, Myles Grant wrote: > >> In my application, I have a common layout >> (''/app/views/layouts/layout.rhtml''). In that file, I want to iterate >> over the 10 most recent NewsPosts to create links for them. I can do >> that by putting: >> >> @recent_news = NewsPost.find(:all, :order => ''created_on DESC'', >> :limit => 10) >> >> in every controller so that @recent_news is accessible to all when >> Rails goes to render the layout. Of course, that''s not very DRY. I >> have tried putting that in application.rb and application_helper.rb >> (and maybe some other places), but the NewsPost model isn''t in scope >> there. >> >> So what''s the proper way to handle this sort of thing? (I apologize >> if this is often-asked, but I can''t find an answer) >> > > What do you mean "not in scope"? The instance variable is not > accessible from the layout or the model class is not defined in your > controller?I can''t put: @recent_news = NewsPost.find(:all, :order => ''created_on DESC'', :limit => 10) in application.rb because the NewsPost model class isn''t defined in there. I think I''m going to use the previous suggestion of components though. Thanks, Myles
Myles, Take a look at the Controller Dependencies here: http://api.rubyonrails.com/classes/ActionController/Dependencies/ClassMethods.html Use: model :news_post at the top of your Application.rb file, then you probably want a filter to run before loading each page, take a look here for those: http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html I would assume you''d only want something like: before_filter :fetch_news, :only=>[:show] That might also be a good use for components though. Try it out ;) .adam sanderson _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails