Last Updated: February 25, 2016
·
1.251K
· chuckbutler

Active Record Models not tied to the DB

Its occasionally necessary to work with Active Record style models (See: Gems that rely heavily on AR) You can circumvent getting messy with your models by scoping them and using the AR::Lint test

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/lint.rb

Quick and Dirty version:

change your class definition as follows:

class Rockies < ActiveRecord::Base
  #Model Code
end

to mimic the following

   class Rockies
      include ActiveModel::Validations
      include ActiveModel::Conversion
      extend ActiveModel::Naming

      def persisted?
          false
       end

   #Model Code
end

Thats pretty much it, If your models were generated using scaffolding this will unlink your model from the database and uses the ActiveModel LINT guide for best uses, its pretty stripped down so if you find a method you need thats not implemented by default, give the official LINT file a glance and see what classes you should include/extend.