Create_association doesn't work after upgrade to Rails 4

165 Views Asked by At

We upgrade Rails 3.2 app to Rails 4 and we have some problems. I have model Account and 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

When I created account

account = Account.create!(name: 'Test Account', ...)

like I did in rails 3 before. I got error with missing account_id in AccountSetting (line: self.create_settings). So I have to change all lines with

self.create_settings

to

self.build_settings.save

. And after I have to reload account everytime when I can use account.settings, because if I don't reload account, I have account.settings without account_id. I don't understand where is the problem and I have same problem with FactoryGirl in test env. It's same problem with all associations (feature_flags, qb_settings,...).

0

There are 0 best solutions below