【发布时间】:2014-05-06 09:57:24
【问题描述】:
我有两个表,课程和类别,并且我有一个带有 course_id 和 category_id 的桥接表分类。我在表 category_rank 中添加了一个附加列,它是一个整数,用于根据用户偏好对类别进行排名。我正在努力在数据库中创建条目。这是我的表格:
create_table "categories", force: true do |t|
t.string "name"
t.boolean "is_active", default: true
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "categorisations", force: true do |t|
t.integer "category_id"
t.integer "course_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "category_rank"
end
create_table "courses", force: true do |t|
t.string "title"
t.datetime "start_date"
t.string "duration"
t.boolean "is_active", default: true
t.integer "state"
t.integer "partner_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "photo"
t.integer "partner"
t.boolean "is_published", default: false
end
这是模型
class Course < ActiveRecord::Base
has_many :categorisations
has_many :categories, :through=> :categorisations
belongs_to :partner
end
class Category < ActiveRecord::Base
has_many :categorisations
has_many :courses, :through=> :categorisations
belongs_to :user
end
class Categorisation < ActiveRecord::Base
belongs_to :category
belongs_to :course
end
当我创建课程时,我使用了一个类别对象列表,这很好用
@course.categories << @categories
但我现在需要为桥接表中的每个类别添加一个排名
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4