我不知道我在做這些正確的。如何將新條目添加到多個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