1

Say I have table with the following columns:

class CreatePosts < ActiveRecord::Migration
  def change
    drop_table :posts

    create_table :posts do |t|
      t.string :First_Name
      t.string :Last_Name
      t.string :Company

      t.timestamps
    end
  end
end

I've been lookin up gems such as faker, Randexp, foregery. But how exactly will the data be imported into the database, So far I've only played around things that go into the database via a form.

I'm assuming it is done through the controller:

def create
  @post = Post.new(post_params)

  @post.save
  redirect_to @post
  end

But not sure how exactly, any examples or hints to guide me through this? Thanks

Thatdude1
  • 905
  • 5
  • 18
  • 31

2 Answers2

2

If this is something that you need to bootstrap your application with, you can either use the seeds file or a rake task which are pretty much the same thing.

Only reason I can think of why you would use Faker for this is if you want to populate your Db for some testing, is that true? If yes, then the approach might vary a little.

  1. Using seeds file:

Open db/seeds.rb file and just use active record methods to start populating data.

Post.create!(First_Name: 'Name', Last_Name: 'Last', Company: 'Company Name')

Once you've written your expected code, just run rake db:seed to populate the db.

Jasdeep Singh
  • 3,276
  • 4
  • 28
  • 47
  • Yes, sorry I am using for testing purposes.I'm a little confused, about where exactly faker would be used in your code above ? – Thatdude1 Sep 27 '13 at 13:20
  • You'd use faker in your create method... e.g. `Post.create!({first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, company: Faker::Company.name})` – Helios de Guerra Sep 27 '13 at 20:37
  • 2
    Also, its highly recommended that you use Ruby/Rails conventions and name your methods(and database columns) with lowercase snakecase names, e.g. first_name rather than First_Name – Helios de Guerra Sep 27 '13 at 20:39
  • You took those words out of my mouth! – Jasdeep Singh Sep 30 '13 at 16:13
0

It really depends on what you're trying to do:

If you just want to populate the db with data for testing, you could use seeds.

Or, you could create a rake task to do this, e.g.

Best way to fill development db in rails

Or create a model to create the data which you could call from the console, e.g. using the 'ffaker' gem...

class RandomPost
  attr_reader :first_name, :last_name, :company

  def initialize options = {}
    @first_name = options[:first_name] ||= Faker::Name.first_name
    @last_name = options[:last_name] ||= Faker::Name.last_name
    @company = options[:company] ||= Faker::Company.name
  end

  def save
    Post.create({first_name: @first_name, last_name: @last_name, company: @company})
  end

end

Then if you need to create multiple random posts in the console you could do...

10.times do
  post = RandomPost.new
  post.save
end
Community
  • 1
  • 1
Helios de Guerra
  • 3,445
  • 18
  • 23
  • Yes I am using this for test data. Supper noob question, but where exactly would i define my RandomPost class ? ... I'm guessing the 10.times would go in the controller, not sure where I would put the class thou ? – Thatdude1 Sep 27 '13 at 13:17
  • You would probably just put it into the models directory (remember not all models have to inherit from ActiveRecord)... The 10.times was a suggestion for use in the console (e.g. 'rails console' from the command line when you're in your project directory)... If you're wanting to create random data from the web interface, you'd want to handle it in a much more flexible way. – Helios de Guerra Sep 27 '13 at 20:34