rails3 has_and_belongs_to_many定制 [英] rails3 has_and_belongs_to_many customization
本文介绍了rails3 has_and_belongs_to_many定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
图书 has_and_belongs_to_many
学生
学生 has_and_belongs_to_many
图书
Book has_and_belongs_to_many
Students
Student has_and_belongs_to_many
Books
在BooksStudents模式,我想添加状态字段来存储如果是租来的,买了..等。并能够选择例如 @ student.books.rented
或 @ student.books.where(:books_students => {:状态=&GT ; 2})
In BooksStudents model I want to add "status" field to store if it is rented, bought ..etc. and be able to select for example @student.books.rented
or @student.books.where(:books_students=>{:status=>2})
我可以做到这一点HABTM?
Can I do that with HABTM?
推荐答案
AFAIK没有,您将需要一个的has_many:通过设置..
AFAIK no, you will need a has_many :through setup..
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(:状态=&GT; 2)
这篇关于rails3 has_and_belongs_to_many定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文