2

Is there any specification I need to stick to on seeds.rb file? When I rake db:seed, I get the response "rake aborted"

I am using rails 5.0.0

Any help will be greatly appreciated. Thanks.

enter code here

bdme551@bdme551:~/bdme/bin$ rails db:seed --trace
Looks like your app's ./bin/rails is a stub that was generated by Bundler.

In Rails 5, your app's bin/ directory contains executables that are versioned
like any other source code, rather than stubs that are generated on demand.

Here's how to upgrade:

  bundle config --delete bin    # Turn off Bundler's stub generator
  rails app:update:bin          # Use the new Rails 5 executables
  git add bin                   # Add bin/ to source control

You may need to remove bin/ from your .gitignore as well.

When you install a gem whose executable you want to use in your app,
generate it and add it to source control:

  bundle binstubs some-gem-name
  git add bin/new-executable

** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
============= WARNING FROM mysql2 =============
The options :user, :pass, :hostname, :dbname, :db, and :sock will be deprecated at some point in the future.
Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options.
============= END WARNING FROM mysql2 =========
rails aborted!
NoMethodError: undefined method `to_i' for {:name=>"FirstName"}:Hash
Did you mean?  to_s
               to_a
               to_h
/home/bdme551/bdme/app/models/user.rb:8:in `initialize'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/inheritance.rb:65:in `new'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/inheritance.rb:65:in `new'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/persistence.rb:33:in `create'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/persistence.rb:31:in `block in create'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/persistence.rb:31:in `collect'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/persistence.rb:31:in `create'
/home/bdme551/bdme/db/seeds.rb:11:in `<top (required)>'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `block in load'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/engine.rb:549:in `load_seed'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:268:in `load_seed'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:196:in `block (2 levels) in <top (required)>'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:248:in `block in execute'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:243:in `each'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:243:in `execute'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/home/bdme551/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:173:in `invoke'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:152:in `invoke_task'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:108:in `each'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:108:in `block in top_level'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:117:in `run_with_threads'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:102:in `top_level'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:178:in `standard_exception_handling'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/app_loader.rb:46:in `require'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/app_loader.rb:46:in `block in exec_app'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/app_loader.rb:35:in `loop'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/app_loader.rb:35:in `exec_app'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/cli.rb:5:in `<top (required)>'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/exe/rails:9:in `require'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/exe/rails:9:in `<top (required)>'
/home/bdme551/.rvm/gems/ruby-2.3.1/bin/rails:23:in `load'
/home/bdme551/.rvm/gems/ruby-2.3.1/bin/rails:23:in `<main>'
/home/bdme551/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/home/bdme551/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
bdme551@bdme551:~/bdme/bin$ 

user.rb

   class User < ActiveRecord::Base
  has_many :roles
  ROLES = {0 => :guest, 1 => :user, 2 => :moderator, 3 => :admin}  

  attr_reader :role

  def initialize(role_id = 0)
    @role = ROLES.has_key?(role_id.to_i) ? ROLES[role_id.to_i] : ROLES[0]
  end

  def role?(role_name)
    role == role_name
  end
end

seeds.rb

    # This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#   Mayor.create(name: 'Emanuel', city: cities.first)

# DELETED - REBASED VERSION!!!

users = User.create([
     { name: 'FirstName' }, { name: 'LastName' }, { user_id: 'userid'} ])

    # {name: "Lecture 8", user_id: 1, link: "http://flatiron-school.s3.amazonaws.com/ruby-003/reviews/playister-associations-with-artists.mp4", description: "Playlister App - Associations with Artists", lecture_date: "10/11/2013"}, 

supplies = Supplies.create([
    { description: 'description' }, { medline_id: 'medline_id' }, { vendor: 'vendor' }, { HCPCS: 'HCPCS' } ])
#   {body: "Installing the **Guard gem** to automatically run rspec tests after each Rails file save action.", video_timestamp: 3783.467413, video_id: 5, created_at: "2013-12-04 20:55:41", updated_at: "2013-12-04 22:08:00", user_id: 3}
# ])

# notes_steph = Note.create([
#   {body: "We use groupings for gems to specify which environments gems are active in.  Here, the rspec, capybara, and selenium gems are grouped in both the test and development environments.", video_timestamp: 2408.407864, video_id: 5, created_at: "2013-12-04 20:54:11", updated_at: "2013-12-04 20:54:11", user_id: 2},
#   {body: "Drawing the routes for the ```Post``` controller via a resource, which generates the 7 standard routes: index, new, create, edit, update, show, and destroy. ", video_timestamp: 4257.238568, video_id: 5, created_at: "2013-12-04 20:58:44", updated_at: "2013-12-04 20:58:44", user_id: 2},
#   {body: "Side-by-side comparison of routes with the `Post` controller.  Shows how a certain route corresponds to a specific action within a controller.", video_timestamp: 5100.092628, video_id: 5, created_at: "2013-12-04 21:00:50", updated_at: "2013-12-04 21:00:50", user_id: 2},
#   { body: "![DHH](http://static.guim.co.uk/sys-images/Technology/Pix/pictures/2007/12/12/heinemeier.article.jpg 'DHH')", video_timestamp: 5661.311041, video_id: 5, created_at: "2013-12-04 21:05:03", updated_at: "2013-12-04 21:05:03", user_id: 2},
#   { body: "\n\n- - Review routes\n- - Relationship between MVC\n- - Basics of CRUD app\n- - Best Practices", video_timestamp: 6357.539163, video_id: 5, created_at: "2013-12-04 21:07:32", updated_at: "2013-12-04 22:00:02", user_id: 2},
#   {body: "**Generating a Rails App** - what's the importance of `-T` ?", video_timestamp: 125, video_id: 5, created_at: "2013-12-04 20:41:41", updated_at: "2013-12-04 22:07:09", user_id: 2},
#   {body: "**Reviewing the Gemfile** - what does the 'turbolink' gem do?", video_timestamp: 590.109342, video_id: 5, created_at: "2013-12-04 20:44:01", updated_at: "2013-12-04 22:07:16", user_id: 2},
#   {body: "**Gem Version Notation:** `~> 4.0.0` means to use any minor version greater than 4.0.0 and less than 4.1.0.  `>= 1.3.0` means to use the most recent version greater than 1.3.0.", video_timestamp: 612.732023, video_id: 5, created_at: "2013-12-04 20:50:02", updated_at: "2013-12-04 22:07:24", user_id: 2},
#   {body: "Differences between the **test, development, and production databases**. Development is a database that's used during the feature development process, whereas production is a database that's used for production purposes.", video_timestamp: 1157.556356, video_id: 5, created_at: "2013-12-04 20:52:15", updated_at: "2013-12-04 22:07:44", user_id: 2},
#   {body: "Installing the **Guard gem** to automatically run rspec tests after each Rails file save action.", video_timestamp: 3783.467413, video_id: 5, created_at: "2013-12-04 20:55:41", updated_at: "2013-12-04 22:08:00", user_id: 2}
# ])

# notes_saron = Note.create([
#   {body: "We use groupings for gems to specify which environments gems are active in.  Here, the rspec, capybara, and selenium gems are grouped in both the test and development environments.", video_timestamp: 2408.407864, video_id: 5, created_at: "2013-12-04 20:54:11", updated_at: "2013-12-04 20:54:11", user_id: 4},
#   {body: "Drawing the routes for the ```Post``` controller via a resource, which generates the 7 standard routes: index, new, create, edit, update, show, and destroy. ", video_timestamp: 4257.238568, video_id: 5, created_at: "2013-12-04 20:58:44", updated_at: "2013-12-04 20:58:44", user_id: 4},
#   {body: "Side-by-side comparison of routes with the `Post` controller.  Shows how a certain route corresponds to a specific action within a controller.", video_timestamp: 5100.092628, video_id: 5, created_at: "2013-12-04 21:00:50", updated_at: "2013-12-04 21:00:50", user_id: 4},
#   { body: "![DHH](http://static.guim.co.uk/sys-images/Technology/Pix/pictures/2007/12/12/heinemeier.article.jpg 'DHH')", video_timestamp: 5661.311041, video_id: 5, created_at: "2013-12-04 21:05:03", updated_at: "2013-12-04 21:05:03", user_id: 4},
#   { body: "\n\n- - Review routes\n- - Relationship between MVC\n- - Basics of CRUD app\n- - Best Practices", video_timestamp: 6357.539163, video_id: 5, created_at: "2013-12-04 21:07:32", updated_at: "2013-12-04 22:00:02", user_id: 4},
#   {body: "**Generating a Rails App** - what's the importance of `-T` ?", video_timestamp: 125, video_id: 5, created_at: "2013-12-04 20:41:41", updated_at: "2013-12-04 22:07:09", user_id: 4},
#   {body: "**Reviewing the Gemfile** - what does the 'turbolink' gem do?", video_timestamp: 590.109342, video_id: 5, created_at: "2013-12-04 20:44:01", updated_at: "2013-12-04 22:07:16", user_id: 4},
#   {body: "**Gem Version Notation:** `~> 4.0.0` means to use any minor version greater than 4.0.0 and less than 4.1.0.  `>= 1.3.0` means to use the most recent version greater than 1.3.0.", video_timestamp: 612.732023, video_id: 5, created_at: "2013-12-04 20:50:02", updated_at: "2013-12-04 22:07:24", user_id: 4},
#   {body: "Differences between the **test, development, and production databases**. Development is a database that's used during the feature development process, whereas production is a database that's used for production purposes.", video_timestamp: 1157.556356, video_id: 5, created_at: "2013-12-04 20:52:15", updated_at: "2013-12-04 22:07:44", user_id: 4},
#   {body: "Installing the **Guard gem** to automatically run rspec tests after each Rails file save action.", video_timestamp: 3783.467413, video_id: 5, created_at: "2013-12-04 20:55:41", updated_at: "2013-12-04 22:08:00", user_id: 4}
# ])

schema.rb

ActiveRecord::Schema.define(version: 20160717055159) do

  create_table "bdme9s", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "role", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string "name", limit: 30, null: false
  end

  create_table "supplies", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string  "Description", limit: 47
    t.string  "Medline_Id",  limit: 20
    t.string  "Manuf_Id",    limit: 20
    t.integer "Qty"
    t.string  "Pkgd",        limit: 7
    t.float   "Price",       limit: 53
    t.string  "Vendor",      limit: 8
    t.string  "HCPCS",       limit: 5
    t.index ["id"], name: "id", unique: true, using: :btree
    t.index ["id"], name: "id_2", using: :btree
  end

  create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string "userid",    limit: 30, null: false
    t.string "FirstName", limit: 20, null: false
    t.string "LastName",  limit: 30, null: false
    t.index ["id"], name: "id", unique: true, using: :btree
  end

end

new error messages

bdme551@bdme551:~/bdme/bin$ rails db:seed --trace
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
============= WARNING FROM mysql2 =============
The options :user, :pass, :hostname, :dbname, :db, and :sock will be deprecated at some point in the future.
Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options.
============= END WARNING FROM mysql2 =========
rails aborted!
NoMethodError: undefined method `create' for User:Class
/home/bdme551/bdme/db/seeds.rb:11:in `<top (required)>'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `block in load'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/engine.rb:549:in `load_seed'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:268:in `load_seed'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:196:in `block (2 levels) in <top (required)>'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:248:in `block in execute'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:243:in `each'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:243:in `execute'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/home/bdme551/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/task.rb:173:in `invoke'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:152:in `invoke_task'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:108:in `each'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:108:in `block in top_level'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:117:in `run_with_threads'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:102:in `top_level'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/rake-11.2.2/lib/rake/application.rb:178:in `standard_exception_handling'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/bdme551/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:seed
bdme551@bdme551:~/bdme/bin$ 
Ajay Barot
  • 1,681
  • 1
  • 21
  • 37
J_A
  • 71
  • 3
  • 14

2 Answers2

2

One of the new features of rails 5 is rake has moved inside rails so use

rails db:seed

instead of

rake db:seed

Also be sure to create the database and run any migrations before trying to seed.

The stack trace indicates that the initialize method is where the error occurs. Your user model has many roles. Presumably you have a role model which 'belongs_to' user. Ideally you will have a role_id column in the user table. If you don't have one, look up running a migrations for references here and set the default value of role_id to '0'. Anytime a user is created without a role id it will be set to zero. Then your seed file should be okay to run as is.

Community
  • 1
  • 1
margo
  • 2,927
  • 1
  • 14
  • 31
  • Thank you Margo for your response. I tried that too. I am getting the following response: rails aborted! NoMethodError: undefined method `to_i' for {:name=>"FirstName"}:Hash Did you mean? to_s to_a to_h /home/bdme5 – J_A Jul 18 '16 at 14:17
  • that sounds like an error in your seed file. What else does the error say. Does it point to a line in seed.rb? – margo Jul 18 '16 at 14:26
  • I am attaching the 'code' rails aborted! NoMethodError: undefined method `to_i' for {:name=>"FirstName"}:Hash Did you mean? to_s to_a to_h /home/bdme551/bdme/app/models/user.rb:8:in `initialize' /home/bdme551/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0/lib/active_record/inheritance.rb:65:in `new' – J_A Jul 18 '16 at 14:31
  • sorry, I am having difficulties attaching the error log. – J_A Jul 18 '16 at 14:32
  • That error points to the user.rb file line 8. Can you post the relevant method from that file. Try editing your question with the full stack trace of the error message and the code from user.rb – margo Jul 18 '16 at 14:33
  • I will post the user.rb as well. – J_A Jul 18 '16 at 14:43
  • what's line 11 in the seed file? – margo Jul 18 '16 at 14:52
  • now I have posted the full stack trace and the code from user.rb (sorry, I am new to stackoverflow and trying to figure out options available). Thanks for your help. – J_A Jul 18 '16 at 14:52
  • users = User.create([ { name: 'FirstName' }, { name: 'LastName' }, { user_id: 'userid'} ]) – J_A Jul 18 '16 at 15:14
  • role_id assignment in the initialize method is confusing. I think you may want to do this in the controller create action. def create User::ROLES.has_key?(params[:role_id].to_i) ? User::ROLES[role_id.to_i] : User::ROLES[0] – margo Jul 18 '16 at 16:23
  • Thank you for analyzing the error log. Do I make the changes in seeds.rb? – J_A Jul 18 '16 at 20:53
  • the `bin/rails db:seed` just calls the `RakeTask named db:seed`, it's still legit to use rake to execute the tasks – siegy22 Jul 19 '16 at 06:26
  • Now that you've supplied better information, I've updated my answer. – margo Jul 19 '16 at 09:02
  • Now, I am getting this error: 'rails aborted! ActiveModel::UnknownAttributeError: unknown attribute 'name' for User.' – J_A Jul 19 '16 at 21:24
  • It sounds like you haven't set up your db tables/migrations. Have you created a migration for User with an attribute of name? You should really start a new question and to close off this one, accept my answer if it heped you. – margo Jul 20 '16 at 08:58
  • @margo I have posted my schema.rb; may be, you can locate the error for me. Please let me know, if you have any additional questions. Thanks a lot. – J_A Jul 21 '16 at 12:55
  • You should really start a new question and to close off this one, accept my answer if it helped you. Do you have a create method in the controller? – margo Jul 21 '16 at 21:58
  • Thanks a lot for helping me. – J_A Jul 22 '16 at 13:20
2

Your error is in the User#initialize. Try to avoid overwriting the initializer of an ActiveRecord::Base Object.

If you do this:

User.create(firstName: "xyz", lastName: "xyz")

The initializer will get called with:

def initialize(role_id = 0)
  role_id # => { firstName: "xyz", lastName: "xyz" }
end

In your case you should create another column storing the role of the user (as a string), or create another table storing the roles.

Solution:

generate a migration:

rails g migration add_role_to_user

open up generated migraton:

class AddRoleToUser < ActiveRecord::Migration
  def change
    add_column :users, :role, :string, default: "guest"
  end
end

in your user model:

class User < ActiveRecord::Base
  def guest?
    role == "guest"
  end
end

as you can see in your schema.rb

The columns of the user table are firstName and lastName. So to create a user you need to use these as keys:

User.create(firstName: "Jack", lastName: "Sparrow")
siegy22
  • 4,295
  • 3
  • 25
  • 43
  • Now, I am getting this error: 'code' rails aborted! ActiveModel::UnknownAttributeError: unknown attribute 'name' for User. – J_A Jul 19 '16 at 18:20
  • @anandamj can you post your `db/shema.rb` or if you can't find that, the `db/structure.sql` – siegy22 Jul 20 '16 at 06:57
  • Thank you for helping me on this. I have posted the schema.rb for your review. Please let me know, where I'm going wrong. Thanks again. – J_A Jul 21 '16 at 12:54
  • Now, I see different errors. I have posted the errors above for your review. I appreciate that. – J_A Jul 21 '16 at 16:48
  • @anandamj I updated the answer, you need to let `User` inherit from `ActiveRecord::Base`: `class User < ActiveRecord::Base` – siegy22 Jul 22 '16 at 05:57