Настройка rails3 has_and_belongs_to_many - PullRequest
2 голосов
/ 08 июня 2011

Книга has_and_belongs_to_many Студенты
Студент has_and_belongs_to_many Книги

В модели BooksStudents я хочу добавить поле «status» для хранения, если оно арендовано, куплено .. и т. Д. и быть в состоянии выбрать, например, @student.books.rented или @student.books.where(:books_students=>{:status=>2})

Могу ли я сделать это с HABTM?

1 Ответ

3 голосов
/ 08 июня 2011

AFAIK нет, вам понадобится 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)

...