2012-06-04 5 views
0

У меня есть моя первая модель Contact с полем :email и мне нужно это же поле :email в моей модели Customer со значением поля :email, который находится в моей модели Contact.Рубин на рельсы доступ к модели информации к другой модели

Я использую Mongoid для ОРМ, так вот моя первая модель Контакт

class Contact 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    embedded_in :customer 
    embedded_in :employee 
    embedded_in :restaurant 

    field :city 
    field :street 
    field :zip_code 
    field :country 
    field :phone_number 
    field :email 

и моя вторая модель клиента

class Customer 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    embeds_one :contact 

    devise :database_authenticatable, :lockable, :recoverable, 
     :rememberable, :registerable, :trackable, :timeoutable, :validatable, 
     :token_authenticatable 

    attr_accessible :email, :password, :password_confirmation 

    field :first_name 
    field :last_name 
    field :password 
    field :gender 
    field :encrypted_password 

Спасибо.

ответ

1

Если вы используете activesupport, делегат должен выполнить эту работу.

В customer.rb

delegate :email, :to => :contact 
0

Вы можете просто написать свой собственный сеттер/геттер

class Customer 
    include Mongoid::Document 

    embeds_one :contact 

    def email 
    contact.email 
    end 

    def email=(string) 
    contact.update_attributes(:email => string) 
    end 
end 
Смежные вопросы