Tableless Model in Rails 5

In this article, I will go over the changes that is required to make the original railscast on Non ActiveRecord Model to work with Rails 5. Create a new Rails 5 project, controllers and the models.

rails new tles
rails g controller letters index show
rails g controller products index show new create edit update destroy
rails g controller categories index show new create edit update destroy
rails g model category name
rails g model product name price:decimal description:text category:references

Define the routes.

Rails.application.routes.draw do
  resources :products, :categories
  get 'letters/:id' => 'letters#show', as: :letters
  get 'alphabets'     => 'letters#index'
  root 'products#index'

Add the following script to seeds.rb.

['Electronics', 'Office Supplies', 'Toys', 'Clothing', 'Groceries'].each do |name|
  Category.create!(name: name)
categories = Category.all
words = File.readlines("/usr/share/dict/words")
lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
350.times do
  Product.create!(name: words.sample.titleize, 
                  category: categories.sample, 
                  description: lorem, 
                  price: [4.99, 9.99, 14.99, 19.99, 29.99].sample)

Migrate and populate the database.

rails db:migrate
rails db:seed

The tableless model letter is shown below:

class Letter
  extend ActiveModel::Naming
  attr_reader :char

  def initialize(char)
    @char = char
  def self.all
    ('A'..'Z').map { |c| new(c) }
  def self.find(param)
    all.detect { |l| l.char == param } 
  # This method is not required because we are not using letter resource
  # def to_param
  #   @char.downcase
  # end
  def products
    Product.where("name LIKE ?", @char + '%').order("name")
  def to_model
    # You will get to_model error, if you don't have this dummy method
  # You need this otherwise you get an error
  def persisted?

You can download the code for this article from with hash 9062df5bf4a0ecb552fc43b6f315875252600d39.


In this article, you learned how to use a tableless model in a Rails 5 app.

Related Articles

Watch this Article as Screencast

You can watch this as a screencast Tableless Model in Rails 5

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.