1

我正在将 Rails 应用程序从 3.2.x 版本升级到 4.0.8,我在 ar 类(位置)上的制造(2.11.3 版)属于类别(还有 HMBT 类别)时遇到了麻烦,而一个类别属于类型学,

class Location
    belongs_to :category
    has_many :location_categories
    has_many :categories, through: :location_categories

    validates :title, :presence => true
    validates :address_city, :presence => true
    validates :nation_id, :presence => true
    validates :phone, :phone_format => true
    validates :mobile, :phone_format => true
    validates :fax, :phone_format => true
    validates :email, :email_format => true
    validates :url, :url_format => true, :allow_blank => true
    validate :validate_category_default
    validates_attachment :logo, size: { in: 0..1.megabytes }, content_type: { content_type: [ 'image/jpg', 'image/jpeg', 'image/png', 'image/gif' ]}, if: Proc.new {|a| a.logo.present? }
    validates_with VersionValidator, :on => :update

    private


    def validate_category_default
        errors.add(:category_ids, I18n.t('activerecord.errors.messages.location.category_default')) unless category_ids.detect{|category| category == category_id }
    end

    # other stuff...
end


class Category
    belongs_to :typology

    validates :typology_id, :uniqueness => { :scope => :site_id }
    validates :title, :presence => true, :uniqueness => { :scope => :site_id }
    validates_with VersionValidator, :on => :update

    # other stuff...
end


class Typology

    validates :logic_title, :presence => true, :uniqueness => true
    validates_attachment :image, size: { in: 0..1.megabytes }, content_type: { content_type: [ 'image/jpg', 'image/jpeg', 'image/png', 'image/gif' ]}, if: Proc.new {|a| a.image.present? }
    validates_with VersionValidator, :on => :update

    # other stuff...
end

然后在相对于位置的制造商上,当创建对象时,它还会触发类别和类型相关的创建:

Fabricator(:location) do
    category(fabricator: :category)
    category_ids { |attrs| [ attrs[:category][:id] ] }
    # other stuff...
end


Fabricator(:category) do
    typology(fabricator: :typology)
    # other stuff...
end

Fabricator(:typology) do
    # other stuff...
end

现在的情况是我检测到在轨道 4 上创建位置时存在问题(在轨道 3 上工作正常),这是堆栈跟踪:

from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/validations.rb:57:in `save!'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:275:in `block in save!'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:330:in `block in with_transaction_returning_status'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in `block in transaction'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `within_new_transaction'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in `transaction'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:209:in `transaction'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:275:in `save!'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/fabrication-2.11.3/lib/fabrication/generator/base.rb:94:in `persist'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/fabrication-2.11.3/lib/fabrication/generator/base.rb:26:in `create'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/fabrication-2.11.3/lib/fabrication/schematic/definition.rb:75:in `block in fabricate'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/fabrication-2.11.3/lib/fabrication/schematic/definition.rb:74:in `instance_eval'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/fabrication-2.11.3/lib/fabrication/schematic/definition.rb:74:in `fabricate'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/fabrication-2.11.3/lib/fabricate.rb:29:in `create'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/fabrication-2.11.3/lib/fabrication.rb:62:in `Fabricate'
from (irb):18
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/railties-4.0.8/lib/rails/commands/console.rb:90:in `start'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/railties-4.0.8/lib/rails/commands/console.rb:9:in `start'
from /Users/my_user/.rvm/gems/ruby-2.1.0/gems/railties-4.0.8/lib/rails/commands.rb:62:in `<top (required)>'
from script/rails:6:in `require'

但我不明白是什么原因造成的。

4

0 回答 0