Tableless model in rails 3.1

前端 未结 6 1680
小蘑菇
小蘑菇 2021-02-04 17:04

Looks like this method doesn\'t work anymore in rails 3.1. So, does someone have a working solution?

Actually, I\'ve found this gist. It solves problems with colum

6条回答
  •  时光取名叫无心
    2021-02-04 17:44

    The simplest tableless model in Rails 3.1 is:

    class Session
      include ActiveModel::Validations
      include ActiveModel::Conversion
      extend ActiveModel::Naming
    
      attr_accessor :email, :password
      validates :email, :presence => true
      validates :password, :presence => true
    
      def initialize(attributes = {})
        if attributes
          attributes.each do |name, value|
            send("#{name}=", value)
          end
        end
      end
    
      def persisted?
        false
      end
    end
    

    The ActiveModel::Validations is optional (only if validations are used). Also the constructor is not required (but highly desireable).

提交回复
热议问题