Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6963491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:47:56+00:00 2026-05-27T15:47:56+00:00

In my seed file I am trying to create 3 users, 1 admin and

  • 0

In my seed file I am trying to create 3 users, 1 admin and 2 default users but it keeps assigning all 3 users to the default role before creation. Here is my code:

User.rb

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  has_many :user_prices
  has_many :products, :through => :user_prices
  validates_presence_of :username, :email, :password, :password_confirmation
  validates_format_of :username, :with => /\A[a-z0-9]{5,20}\z/i
  validates_uniqueness_of :username, :email

  before_create :setup_default_role_for_new_users

  ROLES = %w[admin default]

  private
  def setup_default_role_for_new_users
    if self.role.blank?
      self.role = "default"
    end
  end
end

Seed.rb

puts 'Loading seed data now....'
user1 = User.create(:email => 'admin@email.com', :role => 'admin')
user2 = User.create(:email => 'user1@email.com')
user3 = User.create(:email => 'user2@email.com')
puts 'Users added'

I know user2 and user3 will have the default role but user1 should be admin. How is this done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:47:57+00:00Added an answer on May 27, 2026 at 3:47 pm

    since :role isnt in your accessible attributes, its protected from mass assignment, which is what you are doing in your seed file.

    so in order to set role, you can use something like this

    user1 = User.create(:email => ‘admin@email.com’)
    user1.update_attribute(:role, ‘admin’)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file containing seed data that I'm trying to load into
I'm trying to work with seed-fu gem.I follow the document here . From that
I have an application that is useable by all users (admin or limited) in
I'm trying to seed my database with the standard db/seeds.rb method. This works fine
I am using a System.Random object which is instantiated with a fixed seed all
I have a lot of data that I'm trying to seed into a polymorphic
I'm trying to insert some sample data (Seed data) using EF 4.3 Migration. The
I have the following seeds.rb file: initial_users = User.create( [ { :first_name => Joe,
This has been stumping me for a while. I am trying to create a
I'm trying to write a program which will pseudorandomly autogenerate (based on a seed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.