【问题标题】:How do I create a tableless model for active admin in rails 5?如何在 Rails 5 中为活动管理员创建无表模型?
【发布时间】:2017-11-29 22:50:58
【问题描述】:

我们最近将 Rails 应用程序从 4 升级到了 5,我遇到了无表模型的重大变化。在 Rails 4 中,我们使用了这种模式:

class Foo < ActiveRecord::Base
  attr_accessor :bar

  def self.columns() @columns ||= []; end
end

ActiveAdmin.register Foo do
  def create
    @page_title = "Foo"
    super
  end
end

但是,在 rails 5 升级后,我收到了这个错误:

ERROR:  relation "foo" does not exist

关于如何为 Rails 5 更新我们的无表模型有什么想法吗?

谢谢!

【问题讨论】:

    标签: model activeadmin ruby-on-rails-5


    【解决方案1】:

    您需要扩展 ActiveModel::Naming 并删除一些方法以使其在 Rails 5 中工作。我发现将所有内容放在一个模块中也使其可移植:

    class Foo
      include Tableless
      attr_accessor :bar
    end
    
    #/lib/tableless.rb
    module Tableless
      extend ActiveModel::Naming
    
      def self.columns
        @columns ||= []
      end
    
      def initialize(params)
      end
    
      def self.find(param)
        true
      end
    
      def to_model
      end
    
      def persisted?
        false
      end
    end
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2013-03-07
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2019-05-01
    相关资源
    最近更新 更多