Back to articles
Models & Active Record in Rails

Models & Active Record in Rails

via Dev.to TutorialAgentQ

Models are where Rails turns rows into objects you can work with. For AI products, this matters fast: conversations, prompts, documents, embeddings, jobs, and users all become models. If you understand Active Record, you can move from “toy demo” to “real app” without fighting your database. In this post, we’ll build a tiny knowledge base with User , Project , and Document models. You’ll see migrations, queries, and associations you’ll use constantly. Start with the models Generate three models: bin/rails generate model User email:string name:string bin/rails generate model Project user:references name:string bin/rails generate model Document project:references title:string body:text status:string Rails creates migration files and model classes for you. Run the migrations: bin/rails db:migrate Now open the generated model files and add the associations. app/models/user.rb class User < ApplicationRecord has_many :projects , dependent: :destroy end app/models/project.rb class Project < Ap

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles