Skip to content

Instantly share code, notes, and snippets.

@laktek
Created February 28, 2009 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laktek/72105 to your computer and use it in GitHub Desktop.
Save laktek/72105 to your computer and use it in GitHub Desktop.
has_many relationships
class Student < ActiveRecord::Base
has_many :rating
has_many :books, :through => :ratings
end
class Book < ActiveRecord::Base
has_many :ratings
has_many :students, :through => :ratings
end
class Rating < ActionRecord::Base
belongs_to :book
belongs_to :student
# Schema would be like this
# id :integer
# book_id :integer
# student_id :integer
# vote :boolean ( DEFAULT = NULL )
end
@student.books.find(:all, :condition => ["ratings.vote", true]) #find books student loves
@student.books.find(:all, :condition => ["ratings.vote", false]) #find books student hates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment