2012-10-01 56 views
0

我正在使用rails與數據庫Mongodb。 我正在使用設計。設計有型號名稱user.The ID的用戶是mongodb混亂中的嵌套屬性

: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也裏面保存?

回答

1

您顯示的代碼僅包含個人信息字段,比如出生日期等。我想最簡單的解決方法是將它們放在User類中,並使用內置設計動作(如devise/registrations#edit)對其進行更改,devise/registrations#edit將默認應用current_user的更改。

另外,如果你想保持員工作爲一個單獨的類,你可以嘗試嵌入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類訪問用戶的標識爲user.id