0

deviseを使用していて、ポリモーフィックな関係を作成したいので、テーブルユーザーの「usersable_type」と「usersable_id」に列を追加しました。

これは私のコードです

モデル>>ユーザー

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

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

  #usarsable
  belongs_to :usersable, :polymorphic => true, :autosave => true, :dependent => :destroy
  accepts_nested_attributes_for :usersable

end

モデル>>メディック

class Medic < ActiveRecord::Base
  attr_accessible :license_number, :specialty

  has_one :user, as: :usersable, dependent: :destroy
  has_and_belongs_to_many :patients
end

モデル>>患者

class Patient < ActiveRecord::Base
  belongs_to :socialsecurity
  attr_accessible :birthday, :blood_type
  has_one :user, as: :usersable, dependent: :destroy
  has_many :contacts
  has_and_belongs_to_many :medics
end

Deviseコントローラーをオーバーライドする

class RegistrationsController < Devise::RegistrationsController
  def new
    super
    @user.build_usersable # I had problem in this line
  end

  def create
  end

  def update
    super
  end
end 

これらはすべて私が今持っているモデルですが、それでも同じ問題があります。ポリモーフィックオブジェクトを作成して保存する方法がわかりません。

エラーはまだ同じです

エラー:「<#User:」の未定義のメソッド `build_usersable'

誰かが私を助けることができますか私は感謝します

よろしくお願いいたします。

ジュリ。

4

1 に答える 1

0

コメントの会話に基づいて、これは私があなたが必要だと思う種類の計画です。

アスペクトの概念を使用します。つまり、ユーザーは多くのアスペクトを持つことができます。アスペクトはメディック、コンテンツの詳細などです。アスペクトは1人のユーザーしか持つことができません。

まず、user_idとaspect_typeおよびaspect_idを持つUserAspectモデルが必要です。

class UserAspect < ActiveRecord::Base

  belongs_to :user
  belongs_to :aspect, :polymorphic => true
end

これでユーザーモデル

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

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

  #aspects
  has_many :user_aspects
  has_many :aspects, :through => :user_aspects

end

そして今あなたの医者

class Medic < ActiveRecord::Base
  attr_accessible :license_number, :specialty

  has_one :user_aspect, as: :aspect, dependent: :destroy
  has_one :user, :through => :user_aspect
end

今、あなたは次のようなことをすることができます

user.aspects

medic.user

etc
于 2013-02-08T19:59:43.557 に答える