I'm using Ruby on Rails 3 and i'm attempting to seed data during my application database.

In 'RAILS_ROOT/models/user.rb' I've:

class User < ActiveRecord::Base
  attr_accessible #none

  validates :name,
    :presence => true
  validates :surname,
    :presence => true
  validates :email,
    :presence => true

In 'RAILS_ROOT/db/seed products.rb' I've:

# Test 1
User.find_or_create_by_email (
  :name       => "Test1 name",
  :surname    => "Test1 surname",
  :email      => "test1@test1.test1"

# Test2
User.find_or_create_by_email (
  :name       => "",
  :surname    => "",
  :email      => "test2@test2.test2"

So, running within the Terminal

rake db:seed

obviously the database won't populate with datas because 'attr_accessible' to nil (Situation Test1) and validation not passed (Situation Test2).

I must skip the validation and "attr-accessible effects" throughout the seeding process. Is it feasible? If that's the case, how to achieve that?

P.S.: I'd rather not use within 'RAILS_ROOT/db/migrate/201....rb' code such as this:

execute "INSERT INTO users ( name, surname, email ) VALUES ( "Test1 name", "Test1 surname", "test1@test1.test1")"


I want and to skip all callbacks. Is it feasible? If that's the case, how?

Should you check ActiveRecord's documentation you will see the attributes= method includes a parameter make it possible for this:

attributes=(new_attributes, guard_protected_attributes = true)

Utilize it such as this:

# Create a new user
@user = User.new

# Attributes for the user
@attrib = {
  :name       => "Test1 name",
  :surname    => "Test1 surname",
  :email      => "test1@test1.test1"

# Use 'send' to call the attributes= method on the object
@user.send :attributes=, @attrib, false

# Save the object