rails-snippets-devel#

Gemfile#


Rakefile#

namespace :db do
   task :recreate => [ :drop, :create, :migrate ] do
      if ENV[ 'RAILS_ENV' ] !~ /test|cucumber/
         Rake::Task[ 'db:seed' ].invoke
      end
   end
end

Example of DB load (Hartl books)#

bundle exec rake db:populate
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
admin = User.create!(name: "Example User",
                     email: "example@railstutorial.org",
                     password: "foobar",
                     password_confirmation: "foobar",
                     admin: true)

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
users = User.all()
50.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.microposts.create!(content: content) }
end
end
end
bundle exec rake db:populate --trace

This is what brought the question about

Use

> users = User.all.limit(6)
or just

> users = User.limit(6)

[The 'all' method doesn't take parameters.] (http://apidock.com/rails/ActiveRecord/Scoping/Named/ClassMethods/all)

http requests#

You can add this current_url method in the ApplicationController to return the current URL and allow merging in other parameters#

# https://x.com/y/1?page=1 
# + current_url( :page => 3 )
# = https://x.com/y/1?page=3
def current_url(overwrite={})
    url_for :only_path => false, :params => params.merge(overwrite)
end

Example Usage

>current_url --> http://...
>current_url(:page=>4) --> http://...&page=4