如果我有两个具有 many_to_many 关系的表:
DB.create_table? :students do
primary_key :id
String :name
end
DB.create_table? :books do
primary_key :id
String :title
end
DB.create_table? :students_books do
Integer :num_days_borrowed
foreign_key :student_id,
:students,
:key => :id,
:allow_null => false
foreign_key :book_id,
:books,
:key => :id,
:allow_null => false
end
我有以下续集课程:
class Student < Sequel::Model(:students)
many_to_many :books,
:left_key => :student_id,
:right_key => :book_id,
:join_table => :students_books
def borrowed(bk)
add_book(bk)
end
end
class Book < Sequel::Model(:books)
many_to_many :books,
:left_key => :book_id,
:right_key => :student_id,
:join_table => :students_books
end
所以,现在我可以像这样向学生添加书籍:
s1 = Student.create(:name => 'Hari')
b1 = Book.create(:title => 'Foundation')
s1.borrowed(b1)
我的问题是如何num_days_borrowed
使用 Sequel 模型分配值和检索属性?