Re: has_many

Justin French
2005-09-05T01:51:31Z

Sam Mayes wrote:

> this does as expected and lists all the prices for a system, is
> there an easy way to say only get the first 3 prices? Im a bit stumped

I''d change the relational mapping to get only the records you want,
so instead of:

   has_many :prices

try something like:

   has_many :recent_prices, :order => "id DESC", :limit =>
3, :class_name => "Price"

You can substitute :top_prices for whatever you like...
recent_prices, highest_prices, lowest_prices, newest_prices,
whatever.  Then you just address the association like this:

<% for price in @systems[counter].recent_prices %>
...
<% end %>

See http://api.rubyonrails.com/classes/ActiveRecord/Associations/
ClassMethods.html for more info –– it''s worth the read.


Regards,

---
Justin French
Indent.com.au
TextDrive.com