Question

I am optionally creating User with Nested attributes. I just want to skip confirmation email . But

class ShipperOrder < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
end

class User < ActiveRecord::Base
  has_many :shipper_orders
end

controller

class ShipperOrdersController < ApplicationController
  def create
    @shipper_order = ShipperOrder.new(order_params)
    if @shipper_order.save
        flash[:notice] = "Shipper order created"
    else
        flash.now[:alert] = @shipper_order.errors.full_messages.join(", ")
        render :error
    end
  end

  private

  def order_params
    params.require(:shipper_order).permit({user_attributes: [:first_name, :email,
     :last_name,:mobile_phone, :password, :terms_of_service, :password_reset_required 
     ]}, :description)
  end
end

Now i am creating an order and if user want to assign this order to a User that do not exist in database. Then i am creating that user with help of nested_attributes. Everything works fine . i just want to disable the confirmation email for such type of user.

Was it helpful?

Solution

You can add attr_accessor like

class User < SomeClass

    attr_accessor :skip_user_confirmation

    before_save :skip_confirm


private

  def skip_confirm
    self.skip_confirmation! if self.skip_user_confirmation 
  end

end

IN general it should work, but you might want to change it a little end

And then in controller add to user_attributes key :skip_user_confirmation, which will be true or false

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top