How to create ActiveRecord tableless Model in Rails 3

后端 未结 7 2283
终归单人心
终归单人心 2020-12-30 03:05

I am trying to create a Active Record tableless Model. My user.rb looks like this

class User < ActiveRecord::Base

  class_inheritable_accessor :columns

         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 03:57

    class Tableless
    
      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
    

提交回复
热议问题