我正在使用Ruby on Rails 3,我想使用命名空間類來設置多態關聯。使用命名空間類的多態關聯
遷移是:
create_table :users_users do |t|
t.integer :id
t.string :full_name
t.references :userable, :polymorphic => true
end
create_table :users_profiles do |t|
t.integer :id
...
end
create_table :users_accounts do |t|
t.integer :id
...
end
類是:
class Users::User < ActiveRecord::Base
# Association ...
end
class Users::Profile < ActiveRecord::Base
# Association ...
end
class Users::Account < ActiveRecord::Base
# Association ...
end
如何我必須編寫代碼協會對上述類別爲了汽車(使用:class_name => "Users:User", ...
?) -create and au銷燬關聯的模型記錄,「映射」users_users
表中的那些並反之亦然?
你有什麼建議嗎?我將在userable_type
屬性(例如:'用戶::配置文件','配置文件',...)中具有什麼字符串值?
目前還不清楚你是如何想要這些關係的工作。使用多態你可以用你的遷移來描述它,一個Users :: User將能夠屬於一個Users :: Profile或者一個Users :: Account,但不能同時屬於這兩個。這是你想要的,還是你認爲用戶可以擁有一個配置文件並且可以屬於一個帳戶?如果是這樣的話,多態並不是你需要使用的 - 一個配置文件可能屬於一個用戶,並且帳戶可以通過一個連接表有許多用戶。 – idlefingers 2011-05-04 14:13:07
@idlefingers - 用戶可以有一個配置文件或一個帳戶,而不是兩個。 – user502052 2011-05-04 14:18:22