views:

29

answers:

1

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:integer, 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.

+3  A: 

inside app/models/grinders.rb you should write

class Grinder < ActiveRecord::Base
  def votes_up
    count "choice = 'up'"
  end
end
Peter
I've provided an alternative. but that method should exist...
Peter
Hrm, this is weird, because that didn't work either.
Rickmasta
This is correct code. You can't have things set up properly in your database. To veriy this, replace `grinder.votes_up` with `grinder.count "choice = 'up'"`, and you'll see a failure there too.
Peter
I guess I do not have it set up correctly.
Rickmasta
Honestly, I don't get it.. I believe my database is set up correctly, I added my belongs_to and has_many, ect. I don't get it.
Rickmasta
Got it, it's votes.find_all_by_choice("up")
Rickmasta