预订has_and_belongs_to_many
学生
学生has_and_belongs_to_many
书籍
在 BooksStudents 模型中,我想添加“状态”字段来存储它是否被租用、购买等。并能够选择例如 @student.books.rented
或 @student.books.where(:books_students=>{:status=>2})
我可以使用 HABTM 做到这一点吗?
最佳答案
据我所知不,您将需要一个 has_many :通过设置..
class Book < ActiveRecord::Base
has_many :books_students
has_many :students, :through => :books_students
end
class BooksStudent < ActiveRecord::Base
belongs_to :book
belongs_to :student
end
classStudent < ActiveRecord::Base
has_many :books_students
has_many :books, :through => :books_students
end
所以你可以做类似 @student.books
或 @student.student_books.where(:status =>2)
关于ruby-on-rails - Rails3 has_and_belongs_to_many 定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276156/