ruby - Rails how to order after an instance variable -


in konkurrancerstable have:

rating_score => current score (integer) ratings => number of ratings led score (integer) 

in view want sort konkurrancers after rating. have math: rating_score/ratings ratings may not null because can not divide zero.

<% @konkurrencer.find(:all, :order => '@rating asc', :limit => 5).each |vind| %>   <%= link_to(image_tag(vind.banner2, :style => 'border:none; width:125px; height:125px;'), vind.tracking, :target => '_blank')  %> <% end %> 

my controller:

class publiccontroller < applicationcontroller   helper_method :sort_column, :sort_direction   def index      @konkurrencer = konkurrancer   end end 

my view:

15: <%= @konkurrencer.find(:all, :select => "rating_score/ratings rating", :order => 'rating asc', :limit => 5).each |da| %> 16: <%= da.banner2 %> 17: <% end %> 

and following error:

activemodel::missingattributeerror in public#index showing c:/rails/konkurranceportalen/app/views/public/_konkurrencer.html.erb line #16 raised: missing attribute: banner2 

how create instance variable rating order konkurrancers?

if nothing else, can sort list of records 2 criteria after retrieving them database.

in index method in controller, i'd this:

@konkurrancers = konkurrancer.find(:all, :order => '@rating asc') @konkurrancers.sort! {|a, b|   rel = a.rating_score <=> b.rating_score   # if "rating_scores" equivalent, consider "ratings"   rel == 0 ? a.ratings <=> b.ratings : rel } 

that give list of konkurrencer objects, sorted 2 criteria.

in view, i'd this:

<% @konkurrancers[0,5].each |vind| %>    <%= link_to(image_tag(vind.banner2, :style => 'border:none; width:125px; height:125px;'), vind.tracking, :target => '_blank')  %> <% end % 

that should create html first 5 items in list made in controller. (is wanted?)


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -