如何使用ruby on rails在单个操作中保存不同的表值

时间:2014-05-04 08:53:21

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

如何在单个操作中保存不同的表值???

我想在用户创建操作中保存用户电子邮件表值。

在模型中

class User
  include Mongoid::Document
  field :name, :type => String
  field :email_id, :type => Integer
  field :active, :type => Boolean
  field :status_id, :type => Integer
  has_one :email
  has_one :status
  validates :name, :status_id, :active, :presence => true
  validates :email_id, uniqueness: true
end

class Email
  include Mongoid::Document
  field :email_id, :type => String
  field :type, :type => String
  field :status_id, :type => Integer
  field :old, :type => Boolean
  field :active, :type => Boolean
  field :version, :type => Integer
  has_one :status
  validates :email_id, :type, :version, :status_id, :active, :presence => true
end

class Status
  include Mongoid::Document
  field :name, :type => String
  field :status_type, :type => String
  field :created_at, :type => DateTime
  field :updated_at, :type => DateTime
end

在控制器

def user_create
  @user = User.new(user_params)
  status_id = status.where(:name => "Published").first
  email = Email.new(:email_id => params[:email_id], :type => "User_Email", :version => 1, :active => true, :status_id => status_id.id)
  email.save
  @user.active = true
  @user.status_id = status_id.id
  @user.email_id = email.id
  if @user.save
      redirect_to action: :list
  else
      redirect_to action: :new_user
  end
end

def user_params
  params.require(:user).permit(:name, :alt_name )
end

在调试控制台中

email.id => {BSON::ObjectId} 5365f53f63686511fb010000

始终是用户电子邮件零点

@user.email_id = email.id => 0

因此我无法保存用户ID,因为用户电子邮件ID始终为零

我尝试将BSON ID正常ID转换为时间也不接受

email.id.to_s => "5365f53f63686511fb010000"

只有当我使用mongoid ???

时才能获取id

我必须保存用户表值和电子邮件表值。

我正在使用

  ruby 2.0.0p353
  Rails 4.0.2
  gem 'bson_ext'
  gem 'mongo'
  gem 'mongoid', github: 'mongoid/mongoid'

我必须自动更新电子邮件表格版本。每个用户表更新我想自动更新电子邮件表版本。

如何在电子邮件表中自动维护版本更新?

如何解决这个问题???

1 个答案:

答案 0 :(得分:1)

非常概念。

class User
  include Mongoid::Document
  field :name, :type => String
  field :email_id, :type => Integer
  field :active, :type => Boolean
  field :status_id, :type => Integer
  has_one :email
  has_one :status
  before_create :set_defaults

  accepts_nested_attributes_for :email
  validates :name, :status_id, :active, :presence => true
  validates :email_id, uniqueness: true

  private 
    def set_defaults
      self.active = true
      self.status = Status.where(name:"Published").first
    end
end

class Email
  include Mongoid::Document
  field :email_address, :type => String
  field :type, :type => String
  field :status_id, :type => Integer
  field :old, :type => Boolean
  field :active, :type => Boolean
  field :version, :type => Integer
  has_one :status
  before_create :set_defaults
  validates :email_address, :type, :version, :status_id, :active, :presence => true

  private
    def set_defaults
      self.type = "User Email"
      self.version = 1
      self.active = true
      self.status = Status.where(name: "Published").first
    end
end

def user_create
  @user = User.new(user_params)
  if @user.save
    redirect_to action: :list
  else
    redirect_to action: :new_user
  end
end

def user_params
  params.require(:user).permit(:name, :alt_name, email:{:email_address})
end

您也遇到了问题,因为Email有一个名为email_id的字段为String,该id字段应为Integer User,以便该关联可以正常工作正常。我在我的例子中改变了这一点,你可以看到。现在,当您保存用户时,电子邮件记录也将创建为与{{1}}的关联。