I need some overview thinking help. I have Pictures model/controller but generally, I want to show pictures via a partial displayed in other controller views and I want to do things like choose the picture based upon various concepts of relevance. I also want to update a ''view_count'' column in the Pictures model each time it is displayed. So if in a partial in /users controller, I would love to be able to call some Picture method (either model or controller but probably controller is the more logical choice) but I can''t figure a way to do this... <%= PictureController.some_method %> clearly gives me an error if I try to put that code into a model... <%= Picture.some_method %> I am still up a creek without a paddle this works but clearly doesn''t belong in a view... <% @pic_1 = Picture.find(:first, :conditions => ["active true"], :order => ''view_count''); @pic_1.view_count += 1; @pic_1.save! %> And of course my problem is that I want to use this ''partial'' in a bunch of different controllers & actions which is why I don''t want to include it in each controller/method. What is the best way to handle something like this? Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 14, 6:56 am, Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:> <%= Picture.some_method %> I am still up a creek without a paddle > > this works but clearly doesn''t belong in a view... > > <% @pic_1 = Picture.find(:first, :conditions => ["active > true"], :order => ''view_count''); @pic_1.view_count += 1; @pic_1.save! %> >Why can''t you stick all that in a class method on Picture ? Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
It depends on what you want the method to do. Inside the Picture class, this should work: class Picture < ActiveRecord::Base # ... def self.get_most_viewed_picture pic = Picture.find(:first, :conditions => ["active = ?", true], :order => ''view_count DESC'') pic.increment! :view_count pic end # ... end In the controller: class UsersController < ApplicationController def index @picture = Picture.get_most_viewed_picture end end In the view (/app/views/users/index.html.erb): <h2>Most viewed picture:</h2> <%= image_tag @picture.url %> Should do the trick. If not, please explain a little more. :-) Lasse 2010/2/14 Craig White <craigwhite-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org>> I need some overview thinking help. > > I have Pictures model/controller but generally, I want to show pictures > via a partial displayed in other controller views and I want to do > things like choose the picture based upon various concepts of relevance. > I also want to update a ''view_count'' column in the Pictures model each > time it is displayed. > > So if in a partial in /users controller, I would love to be able to call > some Picture method (either model or controller but probably controller > is the more logical choice) but I can''t figure a way to do this... > > <%= PictureController.some_method %> clearly gives me an error > > if I try to put that code into a model... > > <%= Picture.some_method %> I am still up a creek without a paddle > > this works but clearly doesn''t belong in a view... > > <% @pic_1 = Picture.find(:first, :conditions => ["active > true"], :order => ''view_count''); @pic_1.view_count += 1; @pic_1.save! %> > > And of course my problem is that I want to use this ''partial'' in a bunch > of different controllers & actions which is why I don''t want to include > it in each controller/method. > > What is the best way to handle something like this? > > Craig > > > -- > This message has been scanned for viruses and > dangerous content by MailScanner, and is > believed to be clean. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, 2010-02-14 at 17:06 +0100, Lasse Bunk wrote:> It depends on what you want the method to do. > Inside the Picture class, this should work: > > class Picture < ActiveRecord::Base > # ... > def self.get_most_viewed_picture > pic = Picture.find(:first, :conditions => ["active = ?", > true], :order => ''view_count DESC'') > pic.increment! :view_count > pic > end > # ... > end > > In the controller: > > class UsersController < ApplicationController > def index > @picture = Picture.get_most_viewed_picture > end > end > > In the view (/app/views/users/index.html.erb): > > <h2>Most viewed picture:</h2> > <%= image_tag @picture.url %> > > Should do the trick. If not, please explain a little more.---- pretty much did the trick and in the process, I learned about ''class'' and ''instance'' methods in the model so thanks very much - that was important for me to finally understand. I didn''t know the exact language for what I was looking for so I couldn''t find what I wanted on Google. Once I did... http://railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/ This was extremely valuable to me. The Controller code was something I am trying to avoid. Thanks Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.