The mystical “Tableless” Model for rails

I had a bright idea for an improvement for the app I am currently working on. I have a Rails model that has only 2 rows in the database and the data is never changed, it has quite a lot of relationships and is used quite extensively. My idea was to remove the database out of the equasion and create a Model that would be entirely self dependent. Here I will show you my methods of maddness:

def MyModel include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming def self.attr_accessor(*vars) @attributes ||= [] @attributes.concat( vars ) super end def self.attributes @attributes end def initialize(attributes={}) attributes && attributes.each do |name, value| send("#{name}=", value) if respond_to? name.to_sym end end def persisted? false end def self.inspect "#<#{ self.to_s} #{ self.attributes.collect{ |e| ":#{ e }" }.join(', ') }>" end end

Originally published at kieranandrews.com.au.

Written by

I enjoy building software, making web applications, surfing, snowboarding and cooking!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store