Intern Track Roadmap

ActiveRecord: creating records

This course covers how ActiveRecord creates database rows. It compares .new, which builds an instance in memory, with .create, which builds and saves in one call. It explains .save and its true or false return value, what happens when validation fails, and how errors.full_messages reports the failed rule. It shows when save! and create! should raise ActiveRecord::RecordInvalid. It also names mass assignment, explains why controllers avoid raw params, and shows how to try every method in a console sandbox.

What you'll learn

  • ✓The difference between .new and .create
  • ✓How .save persists a built instance and returns a boolean
  • ✓Reading errors.full_messages after a failed save
  • ✓When to use save! and create! to raise on failure
  • ✓What mass assignment is and why raw params need a safeguard
  • ✓Trying write methods safely in rails console --sandbox
Free preview · Lesson 1 1 min read

.new builds, .create persists

ActiveRecord: model introduction built instances this way. .new never touches the database: persisted? returns false, and id stays nil because no row was assigned one.

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...