Created
February 28, 2009 21:01
-
-
Save laktek/72105 to your computer and use it in GitHub Desktop.
has_many relationships
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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