我不知道我在做这些正确的。如何将新条目添加到多个has_many关联?
我有3个模型,帐户,用户和事件。
帐户包含一组用户。每个用户都有自己的用户名和密码用于登录,但他们可以在同一帐户下访问相同的帐户数据。
事件是由用户创建的,同一帐户中的其他用户也可以读取或编辑它。
我创建了以下迁移和模型。
用户迁移
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.integer :account_id
t.string :username
t.string :password
t.timestamps
end
end
def self.down
drop_table :users
end
end
帐户迁移
class CreateAccounts < ActiveRecord::Migration
def self.up
create_table :accounts do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :accounts
end
end
事件迁移
class CreateEvents < ActiveRecord::Migration
def self.up
create_table :events do |t|
t.integer :account_id
t.integer :user_id
t.string :name
t.string :location
t.timestamps
end
end
def self.down
drop_table :events
end
end
帐户模式
class Account < ActiveRecord::Base
has_many :users
has_many :events
end
用户模式
class User < ActiveRecord::Base
belongs_to :account
end
事件模型
class Event < ActiveRecord::Base
belongs_to :account
belongs_to :user
end
左右....
- 这是设置是否正确?
- 每次用户创建一个新帐户时,系统都会询问用户信息,例如,用户名和密码。我如何将它们添加到正确的表格中?
- 如何添加新事件?
对于这么长的问题,我感到抱歉。我并不十分理解处理这种数据结构的方式。谢谢你们回答我。:)
您确定要自动创建用户名和密码吗?这不是通过表单向用户提供的吗? – Tarscher 2010-05-05 08:03:32
用户在帐号注册过程中需要输入自己的用户名和密码。感谢好友 – 2010-05-05 08:12:58