0

データベースMongodbでレールを使用しています。私はデバイスを使用しています。Devise のモデル名は user です。ユーザーの ID は

:id => current_user.id

データがフォームから保存されるときに、現在のユーザーの ID もコレクションに保存されるようなモデルを作成したいと考えています。私の従業員のモデルは

 class Employee
  include Mongoid::Document
  field :first_name, type: String
  field :middle_name, type: String
  field :last_name, type: String
  field :otherid, type: String
  field :licennum, type: String
  field :citizennum, type: String
  field :licenexp, type: String
  field :gender, type: String
   field :state, type: String
  field :marital_status, type: String
  field :country, type: String
  field :birthdate, type: String
  field :nickname, type: String
  field :description, type: String
  validates_presence_of :first_name

{ }

end

そのモデルからデータが保存されるときに、現在のユーザーIDもその中に保存されるように、中かっこの中に何を入れるべきですか?

4

1 に答える 1

1

表示されるコードには、生年月日などの個人情報フィールドのみが含まれています。最も簡単な解決策は、それらをクラス内に配置し、変更をデフォルトとして適用するUserなどの組み込みのデバイスアクションを使用して変更することだと思います。devise/registrations#editcurrent_user

または、Employee を別のクラスとして保持したい場合は、次のようEmployeeUserクラスに埋め込むことができます。

class User 
  include Mongoid::Document

  embeds_one :employee
  (...)

class Employee
  include Mongoid::Document

  embedded_in :user

この場合、コントローラ レベルでリレーションを設定します。たとえば、次のようになります。

class EmployeesController < ApplicationController

  def create
    current_user.create_employee(params[:employee])
  end

作成後、 Employee クラスからユーザーの ID にアクセスできますuser.id

于 2012-10-01T08:58:24.250 に答える