mp_jr
mp_jr

Reputation: 61

Ruby on Rails Seed Data

I am Ruby/Rails newbie. I am currently learning about the Rails console and databases using Rake and the seeds.rb file.

I am supposed to:

Honestly I don't know how to start working on this. I am supposed to add a post using the rails console or directly from the seeds.rb file? Any guidance will be greatly appreciated.

Upvotes: 5

Views: 7890

Answers (2)

Paweł Dawczak
Paweł Dawczak

Reputation: 9639

Despite the intention of seed-ing - that this is meant to be run once, to populate the database - there is no technical constrain preventing you from running rake db:seed command couple times. Even without cleaning/recreating your database.

In that case, try following code for db/seeds.rb

post_atrributes = [
  { title: "Sample Title 1", body: "Sample body 1" },
  { title: "Sample Title 2", body: "Sample body 2" },
  { title: "Sample Title 3", body: "Sample body 3" },
]

post_attributes.each do |attributes|
  Post.create(attributes) unless Post.where(attributes).first
end

First of all, we're defining an array of attributes for each Post, to be created.

Later, we're iterating through that array (with post_attributes.each do |attributes|), and trying create a new Post, unless one with specified attributes found.

In Rails, there is quite fancy method first_or_create, which does exactly that - queries database for specified attributes (where(attributes)), and if nothing found - creates new record based on provided attributes.

post_atrributes = [
  { title: "Sample Title 1", body: "Sample body 1" },
  { title: "Sample Title 2", body: "Sample body 2" },
  { title: "Sample Title 3", body: "Sample body 3" },
]

post_attributes.each do |attributes|
  Post.where(attributes).first_or_create
end

At this point, you can "seed" the database with rake db:seed and check what is stored in database (after running rails console) by:

Post.all.map(&:title)

Assuming you had empty database before running rake db:seed, it should contain only 3 Posts. The ones specified with attributes in post_attributes.

Now, if you try to modify your db/seeds.rb again, adding an attributes for one more Post:

post_atrributes = [
  { title: "Sample Title 1", body: "Sample body 1" },
  { title: "Sample Title 2", body: "Sample body 2" },
  { title: "Sample Title 3", body: "Sample body 3" },
  { title: "Another Post", body: "WOW!" },
]

post_attributes.each do |attributes|
  Post.where(attributes).first_or_create
end

After running rake db:seed, and checking in console:

Post.all.map(&:title)

You can see, that only one new Post has been created. The one with title "Another Post".

In your question I understood, that when creating new Post, both attributes - title and body have be unique, so if you try to perform the same for attributes like:

post_atrributes = [
  { title: "Sample Title 1", body: "Sample body 1" },
  { title: "Sample Title 1", body: "Sample body 2" },
]

This will create two separate Posts, because they have different body attributes defined.

For Comments you can do similar thing.

Again, as jBeas mentioned earlier - seed-ing has different purpose, but if this is only exercise to play with ActiveRecord - this is one of ways how you can tackle the problem.

Hope that helps!

Upvotes: 6

Darkmouse
Darkmouse

Reputation: 1939

Seeding is the process of populating a database with data.

There are two methods used to accomplish this.

You can use ActiveRecord Migrations

class AddInitialProducts < ActiveRecord::Migration
  def up
    5.times do |i|
      Product.create(name: "Product ##{i}", description: "A product.")
    end
  end

  def down
    Product.delete_all
  end
end

Or you can store it in a separate seeds.rb file

5.times do |i|
  Product.create(name: "Product ##{i}", description: "A product.")
end

afterwards, run rake db:seed

Source

http://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data

Upvotes: 5

Related Questions