Ruby 在 Rails 回调在 Heroku 上不起作用,但在本地起作用

Ruby On Rails callbacks don't work on Heroku, but work locally

我一直在使用 rails_admin 开发学校应用程序。在本地一切正常,但是当我推送到 Heroku 时,我在模型中定义的回调没有被使用,甚至不是最简单的。所以我来找你问是否有人以前遇到过这样的问题或者可以帮助我解释我的日志,因为我找不到原因。

首先,这是我的 Gemfile 的样子:

source 'https://rubygems.org'

ruby '2.2.0'

group :development, :test do
    gem 'railroady'
end

# For documenting models automatically
gem 'annotate', '~> 2.6.6'

# For styling all HTML using SASS
gem 'bourbon'
gem 'neat'
gem 'bitters'

# For using creating SQL triggers inside models
gem 'hairtrigger'

# For creating seed data files from existing data in the database
gem 'seed_dump'

# Used for translations of the mailer and account confirmations
gem 'devise-i18n'

# User permissions and login
gem 'devise'
gem 'cancancan', '~> 1.10'
gem "rolify"
gem "figaro"
gem "rails_admin"

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use sqlite3 as the database for Active Record on the development environment
gem 'sqlite3', group: :development
# Use PostgreSQL as the database for Active Record on the production environment
gem 'pg', group: :production
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
gem 'rails_12factor', group: :production

这是我在其中使用回调的模型:

region.rb

# == Schema Information
#
# Table name: regions
#
#  id         :INTEGER          not null, primary key
#  name       :varchar
#  key        :varchar
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Region < ActiveRecord::Base
  has_many :events, :dependent => :destroy, :inverse_of => :region
  has_and_belongs_to_many :staff_members

  before_create :generate_key

  rails_admin do
    visible do
      bindings[:controller].current_user.roles.first.name == "super_admin"
    end

    list do
      field :id
      field :name
      field :key
    end

    edit do
      field :name do
        required true
        help "Por favor llena este campo."
      end
    end
  end

  private
    def generate_key
      self.key = self.name[0..1]
    end
end

team.rb

# == Schema Information
#
# Table name: teams
#
#  id                   :INTEGER          not null, primary key
#  name                 :varchar
#  key                  :varchar
#  date_of_registration :date
#  company_name         :varchar
#  category_id          :integer
#  address_id           :integer
#  created_at           :datetime         not null
#  updated_at           :datetime         not null
#  winner               :boolean
#

class Team < ActiveRecord::Base
  belongs_to :category, :inverse_of => :teams
  belongs_to :address, :inverse_of => :teams
  has_many :evaluations, :dependent => :destroy, :inverse_of => :team
  has_many :team_members, :dependent => :destroy, :inverse_of => :team
  has_and_belongs_to_many :events

  before_save :generate_key

  def team_label_method
    "#{self.key} - #{self.name}"
  end

  rails_admin do
    object_label_method do
      :team_label_method
    end

    list do
      field :id do
        column_width 40
      end
      field :name do
        column_width 100
      end
      field :key do
        column_width 90
      end
      field :company_name do
        column_width 100
      end
      field :category do
        column_width 70
      end
      field :events do
        column_width 100
      end
      field :winner do
        column_width 10
      end
      field :address do
        column_width 50
      end
    end

    edit do
      field :name do
        required true
        help "Por favor llena este campo."
      end
      #field :key do
      #  required true
      #  help "Por favor llena este campo."
      #end
      field :company_name do
        required true
        help "Por favor llena este campo."
      end
      field :category do
        required true
        help "Por favor llena este campo."
      end
      field :winner do
        default_value = false
        help "Llenar sólo si este equipo es ganador del concurso nacional."
      end
      field :address do
        required true
        help "Por favor llena este campo."
      end
      field :team_members
      field :events do
        required true
        help "Por favor llena este campo."
      end
      field :date_of_registration do
        required true
        help "Por favor llena este campo."
      end
    end
  end

  private
    def generate_key
      standing = "R"
      self.events.each do |current_event|
        if current_event.event_type == "Nacional"
          standing = "N"
        end
      end
      if self.winner == true
        standing = "G"
      end

      region_key = self.events.last.region.key
      year = self.date_of_registration.year
      id = self.id.to_s.rjust(3, '0')
      category = self.category.key
      special = 0

      case self.category.key
        when "S1"
          special = 1
        when "S2"
          special = 2
        when "S3"
          special = 3
        when "S4"
          special = 4
      end

      self.key = "#{standing}#{region_key}#{year}#{id}#{category}#{special}".upcase
    end
end

这是我尝试创建新区域时 Heroku 上日志的样子:

2015-06-02T21:05:45.566768+00:00 heroku[router]: at=info method=GET path="/admin/region?_pjax=%5Bdata-pjax-container%5D" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=887af50d-bfb8-4a96-889d-e858d980838e fwd="189.241.62.189" dyno=web.1 connect=1ms service=415ms status=200 bytes=11925
2015-06-02T21:05:45.512300+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/index.html.haml within layouts/rails_admin/pjax (296.2ms)
2015-06-02T21:05:45.548529+00:00 app[web.1]: Completed 200 OK in 360ms (Views: 321.1ms | ActiveRecord: 22.2ms)
2015-06-02T21:05:47.282570+00:00 heroku[router]: at=info method=GET path="/admin/region/new?_pjax=%5Bdata-pjax-container%5D" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=d8566b9a-943a-4265-afd2-6f339c52c633 fwd="189.241.62.189" dyno=web.1 connect=2ms service=59ms status=200 bytes=3960
2015-06-02T21:05:47.224731+00:00 app[web.1]: Processing by RailsAdmin::MainController#new as HTML
2015-06-02T21:05:47.260190+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/_submit_buttons.html.haml (12.7ms)
2015-06-02T21:05:47.224784+00:00 app[web.1]:   Parameters: {"_pjax"=>"[data-pjax-container]", "model_name"=>"region"}
2015-06-02T21:05:47.272787+00:00 app[web.1]: Completed 200 OK in 48ms (Views: 34.2ms | ActiveRecord: 3.7ms)
2015-06-02T21:05:47.245920+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/_form_field.html.haml (1.3ms)
2015-06-02T21:05:47.222221+00:00 app[web.1]: Started GET "/admin/region/new?_pjax=%5Bdata-pjax-container%5D" for 189.241.62.189 at 2015-06-02 21:05:47 +0000
2015-06-02T21:05:47.260810+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/new.html.haml within layouts/rails_admin/pjax (21.0ms)
2015-06-02T21:05:50.197718+00:00 app[web.1]: Started POST "/admin/region/new" for 189.241.62.189 at 2015-06-02 21:05:50 +0000
2015-06-02T21:05:50.336844+00:00 app[web.1]: Processing by RailsAdmin::MainController#index as HTML
2015-06-02T21:05:50.336854+00:00 app[web.1]:   Parameters: {"model_name"=>"region"}
2015-06-02T21:05:50.447221+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/index.html.haml within layouts/rails_admin/application (96.7ms)
2015-06-02T21:05:50.199887+00:00 app[web.1]: Processing by RailsAdmin::MainController#new as HTML
2015-06-02T21:05:50.199969+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"jajkJUlQhU2i1z0io6pmDe/NZkzB1kA2hGWL8dYauCb22TYFftstrNop57hKTF2Ye0DxS1SNbZt/WQGoHWn+6w==", "region"=>{"name"=>"Prueba"}, "return_to"=>"https://sistema-de-evaluaciones-amte.herokuapp.com/admin/region", "_save"=>"", "model_name"=>"region"}
2015-06-02T21:05:50.224054+00:00 app[web.1]: Redirected to https://sistema-de-evaluaciones-amte.herokuapp.com/admin/region
2015-06-02T21:05:50.224993+00:00 app[web.1]: Completed 302 Found in 24ms (ActiveRecord: 10.2ms)
2015-06-02T21:05:50.334556+00:00 app[web.1]: Started GET "/admin/region" for 189.241.62.189 at 2015-06-02 21:05:50 +0000
2015-06-02T21:05:50.240345+00:00 heroku[router]: at=info method=POST path="/admin/region/new" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=346211ad-fcf1-4012-9239-3884d01ff0a3 fwd="189.241.62.189" dyno=web.1 connect=1ms service=42ms status=302 bytes=1218
2015-06-02T21:05:50.454815+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/layouts/rails_admin/_navigation.html.haml (5.5ms)
2015-06-02T21:05:50.454724+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/layouts/rails_admin/_secondary_navigation.html.haml (4.9ms)
2015-06-02T21:05:50.485504+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/layouts/rails_admin/pjax.html.haml (10.2ms)
2015-06-02T21:05:50.486076+00:00 app[web.1]: Completed 200 OK in 149ms (Views: 114.7ms | ActiveRecord: 24.4ms)
2015-06-02T21:05:50.502507+00:00 heroku[router]: at=info method=GET path="/admin/region" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=e2439dad-9f8d-407b-b3c8-5229bad08b58 fwd="189.241.62.189" dyno=web.1 connect=6ms service=167ms status=200 bytes=16948

这就是我在本地服务器上执行相同操作时所看到的,如您所见,在 POST 块中,很明显 "key" 元素是插入查询。

Started POST "/admin/region/new" for 127.0.0.1 at 2015-06-02 16:10:18 -0500
Processing by RailsAdmin::MainController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"r8fQgZOsomw4C3P/aL/byEqEIY+BnBAm0WpCdNzYPf+n0v1yENTdUlXE/AfT5JBValjfKVmFubfKIQipxSX1qw==", "region"=>{"name"=>"Prueba"}, "return_to"=>"http://localhost:3000/admin/region", "_save"=>"", "model_name"=>"region"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Role Load (0.2ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = ? AND (((roles.name = 'super_admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1]]
  Role Load (0.3ms)  SELECT  "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = ?  ORDER BY "roles"."id" ASC LIMIT 1  [["user_id", 1]]
   (0.3ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "regions" ("name", "created_at", "updated_at", "key") VALUES (?, ?, ?, ?)  [["name", "Prueba"], ["created_at", "2015-06-02 21:10:18.391401"], ["updated_at", "2015-06-02 21:10:18.391401"], ["key", "Pr"]]
   (150.7ms)  commit transaction
Redirected to http://localhost:3000/admin/region
Completed 302 Found in 175ms (ActiveRecord: 152.2ms)

我发现错误了!它与在早期开发阶段直接在 Heroku 上的 PostgreSQL 数据库上创建的 SQL 触发器有关,因为它与回调同时被调用,一次我放弃了它一切都开始按预期工作。