0

I have generated a scaffold in rails to generate a model called transactions which has :

from(int) 
to(int) 
amount(double) 
status(string) 
kind(string) 
start(datetime) 
effective(datetime). 

A form to this effect was automatically created. What I want to know is, is there a way to only get some of these values from the user, and add the others automatically? In this case, from, to, amount and kind need to be entered by the user. status should always be defaulted to "pending", and start should have the current date and time. effective should be null.

3 Answers 3

1

You can do this in many ways

First:

You can use Active Record Callbacks to accomplish this.

Add callback in your model app/models/transaction.rb

before_create :assign_default_attributes

def assign_default_attributes
  self.status = 'pending' if self.pending.blank?
  self.start  = Time.now if self.start.blank?
end

Note: Make sure you remove status, start and effective from permitted params from controller.

Second

Modify app/controllers/transactions_controller.rb create action.

def create
  transaction = Transaction.new(status: 'pending', start: Time.now)
  transaction.assign_attributes(transaction_params)
  if transaction.save
    redirect_to transactions_path, notice: 'Transaction Created'
  else
    flash[:alert] = 'Enter Valid data'
    render :new
  end
end

Note: Make sure you remove status, start and effective from permitted params from controller.

Sign up to request clarification or add additional context in comments.

1 Comment

Would recommend to setup the default values in the migration or Model..Not in controllers.
1

You can add a migration to change the default values:

class ChangeDefaultValueForStatus < ActiveRecord::Migration
  def change
    change_column :transactions, :status, :string, default: "Pending"
    change_column :transactions, :effective, :datetime, default: nil
  end
end

Instead of using start, you can use the in-built timestamps to automatically get the date and time of when a record was created or updated:

class AddTimestampsToTransactions < ActiveRecord::Migration
    def change_table
      add_column(:transactions, :created_at, :datetime)
      add_column(:transactions, :updated_at, :datetime)
    end
end

1 Comment

Thanks. I'm new to rails so this helps
0

i think you need default values

  1. for status,set the default value.

    Add this in your migration to add default value to columns-

    t.string :status, default: "Pending"

check Migration api for more details

  1. For start date,you can set todays time in your form.

       <div class="form_group">
          <label>Start time:</label></br>
          <%= f.datetime_select :starts_at , :class=>"form-control",:start_year => Date.current.year, :end_year => Date.current.year,:selected=> Date.today, :order=> [:day, :month, :year],:start_year=> Time.now.year,:default=> 1.days.from_now,:prompt=> {day: 'Choose day', month: 'Choose month', year: 'Choose year'},:placeholder=>"Enter start time",:required=>true %>
    
        </div>
    

Check the datetime_select api for more details.

2 Comments

Thanks for the answer
Hope it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.