4

For my data model, I have two different types: family members and friends. My plan is to have each have a foreign key to a User table, which is created by Devise. So, when a user signs up, I want them to either go to /friends/sign_up or family_members/sign_up . So, my Friend class is

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

and my User class is

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

I'm also using Formtastic for my view. Right now, I'm getting

unknown attribute: username

with parameters

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

Right now, I'm just randomly trying to add nested_attributes and whatever to the two models. I could use table inhertence, but I'd prefer not to (unless I can add a foreign key to the subclassses pointing at the superclass, that would be fine).

me2
  • 41
  • 1
  • 3

3 Answers3

2

Ok, I've fixed my problem to my satisfaction. Here is my new code for future reference:

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
me-
  • 67
  • 7
0

Try adding this to your Friend model:

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

Have you tried doing something like this:

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
Dex
  • 12,527
  • 15
  • 69
  • 90
0

Are you sure you've added 'username' field to users migration? Devise doesn't add automatically.

Anton Rogov
  • 189
  • 2
  • btw design-wise probably it'd be better to keep all devise logic inside User model and update it so that it has_one or belongs_to friend/family_member - but that's depends on the situation of course :) – Anton Rogov Jul 03 '11 at 08:35