Help me out with a db migration and model in Rails, I tried searching for self referencing and what not, but I can't make heads or tails.. I'm stuck, so to say..
Basically I want two models, User model and Rule model.
In User model I want to specify an owner of user, which is another user, there can only be one owner, user can be owner of itself.
And a Rule model, in which I also want to specify an owner of a rule (User) (User can be an owner of many rules) and user to which this rule applies (User) (User can have many rules).
So I need two migrations and two models, I'll start and hopefully you'll be able to make out of what I'm trying to do ..
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
and
class Rule < ActiveRecord::Base
belongs_to :user # as in owner of rule and rule for user
end
and I'm totally not sure what to write in migrations..
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
#owner?
#stuff left out
t.timestamps
end
end
end
and
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
How do I implement this?