【问题标题】:activerecord-userstamp gem is not workingactiverecord-userstamp gem 不工作
【发布时间】:2016-10-24 11:27:31
【问题描述】:

我正在尝试将 rails 3.x 更新到 4.2.x。我正在尝试用 activerecord-userstamp 替换 userstamp gem 来存储创建者和更新者 ID。

我遵循了github中activerecord-userstamp gem提供的所有步骤并正确配置了gem

   ActiveRecord::Userstamp.configure do |config|
     # config.default_stamper = 'User'
     # config.creator_attribute = :creator_id
     # config.updater_attribute = :updater_id
     config.deleter_attribute = nil
   end

在我重置数据库并进行迁移之后

但我的架构仍未更新为 creator_id、updater_id

      t.integer  "created_by", limit: 4
      t.integer  "updated_by", limit: 4

在我的模型 user.rb 中

     class User < ActiveRecord::Base
      model_stamper
     end

在我的 collections.rb 中

      class Collection < ActiveRecord::Base
       stampable
      end

当我尝试存储 creator_id 和更新的 id 时,它不会存储到数据库中

| created_id | updated_id |
+----+--------+-----------+-
|  NULL      |       NULL |

任何人都可以帮助您解决问题

【问题讨论】:

    标签: ruby-on-rails-4 activerecord rubygems


    【解决方案1】:

    Activerecord-userstamp 在我运行迁移时未能加载 MigrationHelper。我通过将迁移助手添加到我的仓库并在我的迁移文件中使用此助手方法解决了这个问题。

    helper/activerecorduserstamp.rb

    module activeRecorduserstamp
    
      module ActiveRecord::Userstamp::MigrationHelper
      extend ActiveSupport::Concern
    
       def userstamps(*args)
          config = ActiveRecord::Userstamp.config
          column(config.creator_attribute, :integer, *args)
          column(config.updater_attribute, :integer, *args)
          column(config.deleter_attribute, :integer, *args) if config.deleter_attribute.present?
       end
     end
    
      ActiveRecord::ConnectionAdapters::TableDefinition.class_eval do
        include ActiveRecord::Userstamp::MigrationHelper
      end
    end
    

    在我的迁移文件中,我将该辅助方法包含为

    db/migration/yyy.rd

    include activeRecorduserstamp
    class YYY < ActiveRecord::Migration
        def change
          change_table :YYY do |t|
          t.userstamps
        end
    

    在我的迁移文件中添加辅助方法后 .Activerecord-Userstamp 工作得很好。

    参考github issue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-17
      • 2011-09-19
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      相关资源
      最近更新 更多