开发者

How do I make an method for an object in rails?

Hey guys, so I have two models in my project, grinders, and votes.

So each grinder has many votes, and votes belongs to grinder.

The votes table has 3 columns: grinder_id:in开发者_StackOverflowteger, choice:string, and voter_ip:string

How and WHERE can I make a method for my grinders? I want to be able to do something like

<% @grinders.each do |grinder| %>
  <%= grinder.votes_up %>
<% end %>

Where do I define this?

def self.votes_up 
  grinder.votes.find(:all, :choice => "up").count
end

If that is the right way to do it, correct me if I'm wrong, please.


inside app/models/grinders.rb you should write

class Grinder < ActiveRecord::Base
  def votes_up
    count "choice = 'up'"
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜