2011-07-03 111 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