2011-07-03 13 views
4

私のデータモデルには、家族と友人の2種類があります。私の計画は、Deviseによって作成されたUserテーブルへの外部キーをそれぞれ持つことです。だから、ユーザーがサインアップするときに、/ friends/sign_upやfamily_members/sign_upのいずれかに行きたいと思う。だから、私の友達クラスはdeviseの不明な属性

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クラスは、私はまた私のビューのFormtasticを使用してい

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 

です。 は今、私は今のパラメータ

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

unknown attribute: username 

を取得しています、私は無作為に2つのモデルにnested_attributesと何を追加しようとしています。私はテーブルの継承を使用することができますが、私は(スーパークラスを指すサブクラスに外部キーを追加できない限り、それは問題ありません)。あなたの友人のモデルにこれを追加すること

答えて

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

を、あなたは、ユーザーの移行に「ユーザ名」欄が追加されましたか? Deviseは自動的に追加されません。

+0

btw設計上の問題だと思いますが、おそらく、ユーザーモデル内にすべての論理を作り、それを更新してhas_oneやbelongs_to friend/family_memberにするのが良いでしょうが、それはもちろん状況によって異なります:) –

2

[OK]をクリックして問題を解決しました。将来の参照のために私の新しいコードはここにあります:

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 
関連する問題