ActiveRecord::Base Without Table

前端 未结 7 1486
灰色年华
灰色年华 2020-12-01 01:40

This came up a bit ago ( rails model attributes without corresponding column in db ) but it looks like the Rails plugin mentioned is not maintained ( http://agilewebdevelopm

7条回答
  •  执念已碎
    2020-12-01 02:10

    Just an addition to the accepted answer:

    Make your subclasses inherit the parent columns with:

    class FakeAR < ActiveRecord::Base
      def self.inherited(subclass)
        subclass.instance_variable_set("@columns", columns)
        super
      end
    
      def self.columns
        @columns ||= []
      end
    
      def self.column(name, sql_type = nil, default = nil, null = true)
        columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
      end
    
      # Overrides save to prevent exceptions.
      def save(validate = true)
        validate ? valid? : true
      end
    end
    

提交回复
热议问题