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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to