DEV Community

hmza
hmza

Posted on

🧙 Part 4 — ActiveRecord Magic: Rails’ Powerful ORM

🧙 Part 4 — ActiveRecord Magic: Rails’ Powerful ORM

ActiveRecord lets you interact with the database using plain Ruby.

Example migration:


create_table :posts do |t|  
  t.string :title  
  t.text :content  
  t.timestamps  
end  

Enter fullscreen mode Exit fullscreen mode

And in your model:


class Post < ApplicationRecord  
  validates :title, presence: true  
end  

Enter fullscreen mode Exit fullscreen mode

Now, interact easily:


Post.create(title: "Hello Rails!", content: "First post.")  
Post.all  
Post.find(1)  

Enter fullscreen mode Exit fullscreen mode

Top comments (0)