user: - name: string - password: string - has_many: [item,record] item: - content: string - types: integer - belongs_to: [user,item] - has_many: [item,record] record: - content: text - money: number - date: date - belongs_to: [item,user]
Ruby model:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :password
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.string :content
t.integer :types
t.references :user
t.references :item
t.timestamps
end
end
def self.down
drop_table :items
end
end
class CreateRecords < ActiveRecord::Migration
def self.up
create_table :records do |t|
t.text :content
t.decimal :money
t.date :date
t.references :item
t.references :user
t.timestamps
end
end
def self.down
drop_table :records
end
end
本文介绍如何使用 Ruby on Rails 迁移文件创建用户、物品及记录三个模型,并定义了它们之间的关联关系。用户模型包含姓名和密码字段;物品模型包含内容和类型字段,并与用户和其他物品关联;记录模型则包含详细内容、金额、日期等信息,同时关联到物品和用户。
1087

被折叠的 条评论
为什么被折叠?



