Im trying to figure the best way to do this i have a system with many reviews, in the review for a user also gives a ranking (1-10) I wan to do a couple things in my view i want to show last say 5 reviews (easy) and in a seperate area show the average ranking of all reviews. do I need a method in the model that averages all the review rankings for a passed system.id <http://system.id> ? secondly is there a builting for mysql/pg like sum likit etc? thanks for the help Sam _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I''d think you need two instance methods on the class that has_many reviews. def last_five_reviews end def average_ranking end SQL definitely has the ability to do averages. -- -- Tom Mornini On Sep 25, 2005, at 4:55 PM, Sam Mayes wrote:> Im trying to figure the best way to do this > > i have a system with many reviews, in the review for a user also > gives a ranking (1-10) > > I wan to do a couple things > > in my view i want to show last say 5 reviews (easy) > and in a seperate area show the average ranking of all reviews. > > > do I need a method in the model that averages all the review > rankings for a passed system.id ? > > secondly is there a builting for mysql/pg like sum likit etc? > > thanks for the help > > Sam > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On a related note I have systems with many prices models Im trying to find the best price for a system to display in my view i add the following to the prices model def self.bestprice(id) find_first(["system_id = ?", id]) end but when I try this in a view 15: <table width=100% border=0 cellpadding=5 cellspacing=1> 16: <% @bestpr = Price.bestprice(@systems[counter].id) %> 17: <tr> 18: <td bgcolor=#FDFDFD><a href="<%= @bestpr.url %>" class=content_storename1><%= @bestpr.store_name %></a></td> 19: <td align=right bgcolor=#FDFDFD width=50><a href="<%= @bestpr.url %>" class=content_price1>...<%= @bestpr.price %></a></td> 20: </tr> 21: </table> i get an error undefined method `url'' for nil:NilClass I know im doing something stupid just cant figure it out. Thanks Sam On 9/25/05, Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> wrote:> > I''d think you need two instance methods on the class thathas_many reviews. > def last_five_reviews > end > > def average_ranking > end > > SQL definitely has the ability to do averages. > > -- > -- Tom Mornini > > On Sep 25, 2005, at 4:55 PM, Sam Mayes wrote: > > Im trying to figure the best way to do this > > i have a system with many reviews, in the review for a user also gives a > ranking (1-10) > > I wan to do a couple things > > in my view i want to show last say 5 reviews (easy) > and in a seperate area show the average ranking of all reviews. > > > do I need a method in the model that averages all the review rankings for > a passed system.id <http://system.id> ? > > secondly is there a builting for mysql/pg like sum likit etc? > > thanks for the help > > Sam > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
OK I figured it out I needed to check to see if the bestprice returned any values (some dont have any yet), and All is good Sam On 9/25/05, Sam Mayes <codeslave-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On a related note I have systems with many prices models > > Im trying to find the best price for a system to display in my view > i add the following to the prices model > > def self.bestprice(id) > find_first(["system_id = ?", id]) > end > > but when I try this in a view > > 15: <table width=100% border=0 cellpadding=5 cellspacing=1> > 16: <% @bestpr = Price.bestprice(@systems[counter].id) %> > > 17: <tr> > 18: <td bgcolor=#FDFDFD><a href="<%= @bestpr.url %>" class=content_storename1><%= @bestpr.store_name %></a></td> > 19: <td align=right bgcolor=#FDFDFD width=50><a href="<%= @ > bestpr.url %>" class=content_price1>...<%= @bestpr.price %></a></td> > 20: </tr> > 21: </table> > > i get an error > > undefined method `url'' for nil:NilClass > > > > I know im doing something stupid just cant figure it out. > > Thanks > > Sam > > > On 9/25/05, Tom Mornini < tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> wrote: > > > > I''d think you need two instance methods on the class that has_many > > reviews. > > def last_five_reviews > > end > > > > def average_ranking > > end > > > > SQL definitely has the ability to do averages. > > > > -- > > -- Tom Mornini > > > > On Sep 25, 2005, at 4:55 PM, Sam Mayes wrote: > > > > Im trying to figure the best way to do this > > > > i have a system with many reviews, in the review for a user also gives a > > ranking (1-10) > > > > I wan to do a couple things > > > > in my view i want to show last say 5 reviews (easy) > > and in a seperate area show the average ranking of all reviews. > > > > > > do I need a method in the model that averages all the review rankings > > for a passed system.id <http://system.id> ? > > > > secondly is there a builting for mysql/pg like sum likit etc? > > > > thanks for the help > > > > Sam > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails