Domanda

How to save different table values in single action???

I Want to save User and Email table values in user create action.

In Model

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

In Controller

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

In Debug console

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

always user email taking Zero

@user.email_id = email.id => 0

so I cant save user id, because user email id taking zero always

i try to convert BSON id normal id that time also its not taking

email.id.to_s => "5365f53f63686511fb010000"

How to take id only when i'm using mongoid ???

I have to save User table values and Email table values.

I'm using

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

i have to update email table version automatically. Every user table update i want to update email table version automatically.

How to maintain automatically versions update in email table ??

How to fix this issues ???

È stato utile?

Soluzione

Very General Concept.

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

You are also experiencing Issues because Email has a field called email_id as a String which should be id as an Integer for the association to work properly. I changed this in my example so you can see. Now when you save a user the email record will also be created as an association to the User.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top