Rails3 has_and_belongs_to_many 定制

发布于 2024-11-14 07:22:30 字数 289 浏览 5 评论 0原文

预订has_and_belongs_to_many学生
Student has_and_belongs_to_many BooksStudents 模型中的书籍

我想添加“状态”字段来存储它是否被租用、购买等。并能够选择例如 @student.books.rented@student.books.where(:books_students=>{:status=>2})

可以我用 HABTM 来做吗?

Book has_and_belongs_to_many Students
Student has_and_belongs_to_many Books

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})

Can I do that with HABTM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野生奥特曼 2024-11-21 07:22:30

AFAIK 不,你将需要一个 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(:status =>2)

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

so you can do something like @student.books or @student.student_books.where(:status =>2)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文