1

I have a weird bug that just popped up with my rails app that I cannot figure out. I recently added a new association to an existing Model and now my previous associations do not want to work properly.

#=> self.user
#=> <# user.id => "1" ...
#=> self.transactions
#=> [<# transaction_id => "1"...
#=> self.credit_plan
#=> nil

So the first two associations work fine via, but for some reason credit_plan returns nil and is crashing all my existing working code. Here is the record associations I have.

class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :credit_plan
  has_many :transactions, :class_name => "OrderTransaction"

.

class CreditPlan < ActiveRecord::Base
  scope :active, where({:is_active => true})
  scope :inactive, where({:is_active => false})
  has_many :orders, :class_name => "Order"

.

class OrderTransaction < ActiveRecord::Base
  belongs_to :order
  serialize :params
2
  • 1. Did you restart the server after adding the association? 2. Did you migrate, adding the credit_plans table, and adding credit_plan_id to orders? 3. :class_name => "Order" is redundant.
    – bricker
    Oct 14, 2011 at 3:19
  • @bricker 1. Yes 2. Yes 3. I thought it was needed, but I'll try and take it out and see what that is. Oct 14, 2011 at 21:01

1 Answer 1

1

Alright Guys, I figured it out. If I had posted more context of my files, I'm sure someone would have figured it out and helped me sooner.

So basically, when I was setting up my virtual attributes for the credit card form, I accidentally stomped on my own name space by adding :credit_plan as an attribute, which overrides the association.

class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :credit_plan
  has_many :transactions, :class_name => "OrderTransaction"

  validates_presence_of :credit_plan_id, :user

  attr_accessor :first_name, :last_name, :card_type, :credit_card,
            :number, :verification_value, :promotional_code, :expires_on,
            :credit_plan # << This will override associations, delete to fix. 

  validate :validate_card, :on => :create

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.