制作宝石:与属性一起使用的关联问题

时间:2014-07-29 20:33:45

标签: ruby-on-rails-4 fabrication-gem

我正在将rails应用程序从版本3.2.x升级到4.0.8,我在制作(版本2.11.3)上的ar类(位置)属于类别(而且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

然后在相对于Location的制造者上,当创建一个对象时,它还会触发类别和类型学相关的创建:

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'

但我不明白是什么导致它的。

0 个答案:

没有答案