can't populate db using rake db:populate - ruby-on-rails

I am trying to populate my db using rake db:populate. I am on chapter 10.3.2 on michael hartl's book.
Even though I don't get any error messages the DB doesn't seem to be populating.
This is the sample_data.rake file I created:
namespace :db do desc "Fill database with sample data" task populate: :environment do
User.create!(:name => "Example User",
:email => "example#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end

Stopping your server before resetting and repopulating your db should work.

Related

devise user from rails console

I am trying to create a devise user using rails console with the below method
User.create!({:email => "abc#gmail.com", :roles => ["admin"], :password => "abc123", :password_confirmation => "abc123" })
I am not able to understand If the user is created or not. After I hit enter command I do not get any respond. I did user.save and then did u.all but it shows nothing and I am not able to login.
Please suggest what's wrong.
Edit: I am trying below as well but it is not working
#user = User.new(:email => 'test#example.com', :password => 'password', :password_confirmation => 'password')
#user.save
Thanks
UPDATED:
You are missing the {} in your User.new()
user = User.new({email: 'test#example.com', password: 'password', password_confirmation: 'password'})
user.save
Do you have :confirmable option on? If yes, do:
user = User.new({email: 'test#example.com', password: 'password', password_confirmation: 'password'})
user.skip_confirmation!
user.save

Rails Sample_App, Test errors

So following the Hartl sample app tutorials, and having a bit of trouble getting some tests to past regarding the relationship spec.
in my spec file I have
describe "follow methods" do
before(:each) do
#follower = User.create!( :name => "Example User",
:email => "user#example.com",
:password => "foobar",
:password_confirmation => "foobar"
)
#followed = User.create!( :name => "Example User",
:email => "user_five#example.com",
:password => "foobar",
:password_confirmation => "foobar"
)
#attr = { :followed_id => #followed.id }
#relationship = #follower.relationships.create!(#attr)
end
I know it's not the same as in the book however I'm not using FactoryGirl.
When using this block for tests I am getting an error with my #attr which reads;
Failure/Error: #attr = { :followed_id => #followed.id }
NoMethodError:
undefined method `id' for #<Hash:0x00000108d0b2a0>
However, when I run this block in my console it works just fine. The id is set according to the #attr, and I can call .id on #followed.
Any help would be much appreciated as always!
According to your error, the method id does not exist for a Hash.
To access that, you can use this:
#attr = { :followed_id => #followed['id'] }
The problem was you were trying to do #followed.id

Unable to create user model in Rspec

When I run the following spec:
require 'spec_helper'
describe User do
before :each do
#user = User.new :email => "foo#bar.com", :password => "foobar", :password_confirmation => "foobar"
end
it "should be valid" do
#user.should be_valid
end
end
I get this error:
1) User should be valid
Failure/Error: #user = User.new :email => "foo#bar.com", :password => "foobar", :password_confirmation => "foobar"
ActiveRecord::UnknownAttributeError:
unknown attribute: email
# ./spec/models/user_spec.rb:5:in `new'
However, when I go in to console and run
user = User.new :email => "foo#bar.com", :password => "foobar", :password_confirmation => "foobar"
user.valid?
It returns true. For some reason, in my test, I am unable to create a User instance, saying that the email attribute is inaccessible.
Console uses the development database, but specs use the test database. Make sure email is defined in both.

Railstutorial: db:populate vs. factory girl

In the railstutorial, why does the author choose to use this (Listing 10.25):
http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
User.create!(:name => "Example User",
:email => "example#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
to populate the database with fake users, and also (Listing 7.16)
http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "mhartl#example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
It appears that both ways creates users in the database right (does factory girl create users in the database)? What is the reason for the two different ways of creating test users, and how are they different? When is one method more suitable than the other?
Faker and Factory Girl are being used in those examples for two different purposes.
A rake task is created using Faker to easily let you populate a database, typically the development database. This lets you browse around your app with lots of populated, fake data.
The factory definition makes tests convenient to write. For example, in your RSpec tests you can write:
before(:each) do
#user = Factory(:user)
end
Then #user is available in the tests that follow. It will write these changes to the test database, but remember that these are cleared each time you run tests.

Rails 3: Generate Fake Data to Populate DB

I am using faker to generate sample data. I have this as follows:
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
User.create!(:name => "rails",
:email => "example#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
#name = Faker::Name.name
name = "rails#{n+1}"
email = "example-#{n+1}#railstutorial.org"
password = "password"
user = User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
The problem is that I have a couple of after_save callbacks that are not being called when the User is created. Why is that? Thanks
The methods:
after_save :create_profile
def create_profile
self.build_profile()
end
In all my reading, it seems that save! bypasses any custom before_save, on_save or after_save filters you have defined. The source code for create! reveals that it invokes save!. Unless you absolutely NEED the bang version, why are you using it? Try removing all your bang methods and just invoking the non-bang versions:
[1..100].each do |n|
name = "rails#{n+1}"
email = "example-#{n+1}#railstutorial.org"
password = "password"
user = User.new(:name => name, :email => email, :password => password, :password_confirmation => password)
if !user.save
puts "There was an error while making #{user.inspect}"
end
end

Resources