Active Record Basics issue

I’m not entirely sure how to handle this section in Active Record Basics:

user = User.create(name: “David”, occupation: “Code Artist”)

* ```
user = = "David"
user.occupation = "Code Artist"

user = do |u| = “David” u.occupation = “Code Artist” end

I'm pretty sure those aforementioned scripts are meant to be run in rails console.  However, am I supposed to declare 'user' as a method in one of the **ActiveRecord model files**?

**Here is the error message I am receiving in console**:

user = User.create(name: "David", occupation: "Code Artist")
nnection_adapters/sqlite3_adapter.rb:387:in `table_structure': Could not find ta
ble 'users' (ActiveRecord::StatementInvalid)

Any insight would be very helpful.  Thank you

It seems like you haven’t created the “users” table in the database. An ActiveRecord model needs to be backed by a database table. This guide covers the basics of Rails, including creating tables via migrations.

If you have only a database table and no other Rails code yet, you can use The Brick gem to auto-create a model for you based on any existing tables. If you’re using Sqlite then here’s all the steps in order to build out such a table:

  1. Open the sqlite3 interface pointed to your development database by typing this in Terminal:
sqlite3 db/development.sqlite3
  1. Create the users table by pasting in this command:
  name TEXT,
  occupation TEXT,
  password_hash TEXT,
  max_login_attempts INTEGER DEFAULT 5,
  must_change_password INTEGER DEFAULT 0, -- Same as boolean "false"
  created_at TIMESTAMP,
  updated_at TIMESTAMP

And if you’re following along through the rest of the tutorial then you might want to also create a publication_types table :slight_smile:

CREATE TABLE publication_types (
  name TEXT

Then quit out of this interface with simply .quit.

  1. In your Rails project, add this to the Gemfile:
gem 'brick'
  1. After doing a bundle, run bin/rails s. Now navigate to http://localhost:3000/users, and you will see your table shown. You can add a few rows of data by using the “New User” link.

  2. After exiting the rails s by pressing CTRL-C, you can now run bin/rails c and then put in the commands you mentioned above to create a user object with Ruby code. They should work just fine, as well as any other ActiveRecord stuff.

Note that The Brick makes the model file for you (as well as controller and views), but just in RAM – not on the disk. So you can override the auto-created ones by putting your own hand-coded model / controller / view files into place, and do things on your own the real Rails way.

If you create all your own files then you can remove The Brick gem entirely and rely on your own code. Thus one use for this gem is as “training wheels” in order to get started.

Have fun!