升级到Rails 4后,Create_association不起作用

时间:2014-12-16 13:57:12

标签: ruby-on-rails-4 factory-bot rails-3-upgrade

我们将Rails 3.2应用程序升级到Rails 4,我们遇到了一些问题。我有模型帐户和AccounSettings:

class Account < ActiveRecord::Base

  # ...
  after_commit :create_default_settings, on: :create

  # associations
  has_one :feature_flags, class_name: "FeatureFlag"
  has_one :qb_settings, class_name: "QbSetting"
  has_one :settings, class_name: "AccountSetting"
  has_one :pos_settings, class_name: "PosSetting"
  # ...

  def create_default_settings
    self.create_settings
    self.create_feature_flags(qb_enabled: false, qb_online_enabled: false)
    self.create_default_taxes
    self.create_qb_settings(tz: self.tz)
    self.create_pos_settings(default_tax_id: SalesTax.where(account_id: self.id).first.id)
    self.qb_settings.set_qb_sync_attributes_with_date(Time.zone.now)
  end

  def create_default_taxes
    sales_tax = SalesTax.create!(name: "State", description: "State Tax", tax_rate: 0.085, tax_agency: "Franchise Tax Board", account_id: self.id) 
  end

  # ...
end


class PosSetting < ActiveRecord::Base
  # ...

  belongs_to :account

  belongs_to :default_tax, class_name: "SalesTax"
  belongs_to :default_term_code, class_name: "TermCode"

  belongs_to :default_sales_rep, class_name: 'User'

  belongs_to :payment_gateway

  def is_metric?
    measurement_system == 'metric'
  end

  enumerize :measurement_system, in: %w(english metric), default: 'english'
  enumerize :accounting_method, in: %w(accrual cash), default: 'cash'

end

当我创建帐户时

  

account = Account.create!(名称:'测试帐户',...)

就像我之前在rails 3中做过的那样。我在AccountSetting中缺少account_id时出错(line:self.create_settings)。所以我必须用

更改所有行
  

self.create_settings

  

self.build_settings.save

。每当我可以使用account.settings时我必须重新加载帐户,因为如果我不重新加载帐户,我有account.settings没有account_id。我不明白问题在哪里,我在测试环境中与FactoryGirl有同样的问题。所有关联(feature_flags,qb_settings,...)都存在同样的问题。

0 个答案:

没有答案