帮助我在 Rails 中进行数据库迁移和模型,我尝试搜索自引用,但我无法做出正面或反面.. 我被卡住了,可以这么说..
基本上我想要两个模型,用户模型和规则模型。
在用户模型中,我想指定用户的所有者,即另一个用户,只能有一个所有者,用户可以是自己的所有者。
还有一个规则模型,我还想在其中指定规则的所有者(用户)(用户可以是许多规则的所有者)和该规则适用的用户(用户)(用户可以有许多规则)。
所以我需要两个迁移和两个模型,我会开始,希望你能理解我正在尝试做的事情..
class User < ActiveRecord::Base
belongs_to :user #?
has_one :user # as in owner
has_many :rules # rules for given user and rules that are created by this user
... #and some more similar entries
end
和
class Rule < ActiveRecord::Base
belongs_to :user # as in owner of rule and rule for user
end
而且我完全不确定在迁移中要写什么..
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
#owner?
#stuff left out
t.timestamps
end
end
end
和
class CreateRules < ActiveRecord::Migration
def change
create_table :rules do |t|
t.string :title
#rule for?
#owner?
#stuff left out
t.timestamps
end
end
end
我该如何实施?