0

How do I add data to a table in Rails?

So far I have created a Rails app that pulls in data from an API. Next, I have ran the command

rails generate model order

and I have /db/migrate/timestamp_create_orders.rb

class CreateOrders < ActiveRecord::Migration
  def change
    create_table :orders do |t|
      t.string :email, null: false
      t.string :order_date, null: false
      t.string :total_price, null: false
      t.string :order_number, null: false
      t.string :cust_name, null: false
      t.string :zip_code, null: false

      t.timestamps null: false
    end
  end
end

Next, I believe I need to run db:migrate and that will create the table.

My question is, how do I add data to this table? I want the user to visit the app, it pulls in the data, and stores it in this table.

I have found conflicting advice.. Should I just use the advice from here

Order.create(:email=>'[email protected]',:order_data=>"... 

But other advise seems to say not to do this here and here. Though they are all pretty old

4
  • 1
    Have looked at the rails docs? guides.rubyonrails.org/…. Model.create() will create and save the record. Model.new() will create it and you would have to do var.save to save to the db. Commented Oct 6, 2016 at 1:23
  • Thanks. So this is best practice? I didn't really understand why some posts were saying you shouldn't use this. Only don't use it for seed data? Commented Oct 6, 2016 at 1:25
  • 1
    Are you wanting to save data coming from the api while the app its in use ( that seems to be your question the rest is setup)? Seeding a database with default data vs saving records while an app is in use are two different things (to me at least). I would not add data to a db through a migration. Its for creating and modifying tables (db setup). Your controller,models,api,seed,etc files would handle adding and removing data depending on your prupose/goal. Id read the whole doc page above, it explains with examples and gives context. Commented Oct 6, 2016 at 9:54
  • 1
    Sorry, to answer your question in your comment, I think thats right, they were saying not to use the create() method in migrations which are Rails' way of manipulating the database structure, but its fair in your app code. Commented Oct 6, 2016 at 11:25

2 Answers 2

2

You do not create database entries in migrations, you usually create schema or specify changes in the schema in migration files. You use seeds for creating seed data in the database.

To create new data in database through rails you can use either create or new method but you need to save the data as mentioned in other posts in your links when you are using new method.

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

Comments

1

While creating or migrating a new database table, table row is not automatically added. You need to add them manually. One way to populate the newly created database table is using seeds.rb file which is located in your application db folder. You can add Faker gem to your application for creating fake table attribute elements. An example using faker:

(1..3).each do # it'll create  3 new order
   Order.create(email: Faker::Internet.email, order_date: Faker::Date.between(2.days.ago, Date.today))
end

Then run rake db:seed in your project folder console.

If you have some validation in your order.rb file, then you can create new instance of that order and then save it like:

order = Order.new(....)

order.save(validate: false)

Comments

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.