2011-07-03 153 views
4

對於我的數據模型,我有兩種不同的類型:家庭成員和朋友。我的計劃是讓每個都有一個由Devise創建的用戶表的外鍵。所以,當用戶註冊時,我希望他們去/ friends/sign_up或family_members/sign_up。所以,我的朋友類是設計的未知屬性

class Friend < ActiveRecord::Base 
    belongs_to :label 
    belongs_to :user 
    belongs_to :gender 
    belongs_to :location 
    belongs_to :orientation 
    belongs_to :matchmaker 

    def after_initialize 
    self.build_user if self.user.nil? 
    end 
    #accepts_nested_attributes_for :user 
    delegate :username, :to => :user 
    delegate :email, :to => :user 
    delegate :password, :to => :user 
    delegate :password_confirmation, :to => :user 
    delegate :rememebr_me, :to => :user 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
    # Setup accessible (or protected) attributes for your model 
    attr_accessible :username, :email, :password, :password_confirmation, :remember_me 
end 

和我的User類是

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable,  :confirmable, :lockable, :timeoutable and :omniauthable 
# devise :database_authenticatable, :registerable, 
#   :recoverable, :rememberable, :trackable, :validatable 

    attr_accessor :username, :email, :password, :password_confirmation, :remember_me 
    # Setup accessible (or protected) attributes for your model 
    attr_accessible :username, :email, :password, :password_confirmation, :remember_me 
end 

我還使用Formtastic對我的看法。 現在,我越來越

unknown attribute: username 

與參數

{"utf8"=>"✓", "authenticity_token"=>"8ScsJebuCWnflaRQkp9MsBuaaqfzQKaZBXotLyNwNyM=", 
"friend"=>{"username"=>"aaaa", 
"email"=>"[email protected]", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}, 
"commit"=>"Create Friend"} 

現在,我只是隨意嘗試添加nested_attributes和任何兩個型號。我可以使用表Inhertence,但我不希望(除非我可以添加一個外鍵指向超類的子類,那很好)。

回答

0

嘗試增加給你的朋友型號:

attr_accessible :username, :email, :password, :password_confirmation, :remember_me 

您是否嘗試過做這樣的事情:

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
end 

class Friend < User 
    belongs_to :label 
    belongs_to :user 
    belongs_to :gender 
    belongs_to :location 
    belongs_to :orientation 
    belongs_to :matchmaker 
end 

class FamilyMember < User 
end 
+0

我實際上在我的代碼中,它只是做了一個壞的複製粘貼作業。 – me2

0

您確認您已加入「用戶名」字段的用戶遷移?設計不會自動添加。

+0

btw設計明智可能最好將用戶模型中的所有設計邏輯保留在用戶模型中並更新它,以便has_one或belongs_to friend/family_member - 但這取決於當然的情況:) –

2

好的,我已經解決了我的問題,讓我滿意。這裏是我的新代碼供將來參考:

class Friend < ActiveRecord::Base 
    belongs_to :friend_user, :class_name => 
end 

class FriendUser < User 
    set_table_name :users 
    has_one :friend 
end 
class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :username, :email,  :password, :password_confirmation, :remember_me 
end