views:

34

answers:

2

EDIT: This is with Rails 2.3.5

For some reason, when I try to associate a CreditCard with Band in my project, I get the following errors

SQLite3::SQLException: table credit_cards has no column named band: INSERT INTO "credit_cards" ("created_at", "vault_token", "billing_zipcode", "billing_first_name", "updated_at", "band", "billing_state", "billing_city", "id", "billing_email_address", "billing_country", "billing_address", "billing_phone_number", "billing_last_name") VALUES ('2010-10-08 03:26:07', 'vaulttokenvalue', 12345, 'Test', '2010-10-08 03:26:07', 'beekin', 'TX', 'AnyCity', 714867248, '[email protected]', 'US', '1234 Any Street', '5555555555', 'User')

and

SQLite3::SQLException: table bands has no column named credit_card: INSERT INTO "bands" ("name", "created_at", "next_payment_date", "updated_at", "credit_card", "account_type_id", "id", "hometown_city", "account_status", "subdomain", "hometown_state", "website", "account_type_name") VALUES ('Beekin', '2010-10-08 03:29:16', '2010-10-07', '2010-10-08 03:29:16', 'card_one', 715507355, 862144657, 'Nashville', 'ACTIVE', 'beekin', 'TN', 'http://www.beekin.com', 'MONTHLY')

My migration looks like

class CreateCreditCards < ActiveRecord::Migration
  def self.up
    create_table :credit_cards do |t|
      t.references :band
      t.string :billing_first_name
      t.string :billing_last_name
      t.string :billing_address
      t.string :billing_city
      t.string :billing_state
      t.string :billing_zipcode
      t.string :billing_country
      t.string :billing_email_address
      t.string :billing_phone_number
      t.string :vault_token

      t.timestamps
    end

    add_column :bands, :credit_card_id, :integer
  end
end

My bands.yml fixture looks like:

beekin:
  name: Beekin
  website: "http://www.beekin.com"
  hometown_city: Nashville
  hometown_state: TN
  subdomain: beekin
  account_type: monthly
  account_type_name: "MONTHLY"
  account_status: "ACTIVE"
  credit_card: card_one
  next_payment_date: <%= Date.today %>

And my credit_cards.yml fixture looks like:

card_one:
  band: beekin
  billing_first_name: Test
  billing_last_name: User
  billing_address: "1234 Any Street"
  billing_city: AnyCity
  billing_state: TX
  billing_zipcode: 12345
  billing_country: US
  billing_email_address: "[email protected]"
  billing_phone_number: "5555555555"
  vault_token: vaulttokenvalue

Can someone PLEASE point out what I'm doing wrong. I've compared it to other fixtures and it looks like it should work, but it's not.

Have the has_one :credit_card (in band model) and belongs_to :band (in credit_card model). I'm just really lost now.

A: 

Hi try

card_one:
  band_id: 1 //or any other Band id
  billing_first_name: Test
  billing_last_name: User
  billing_address: "1234 Any Street"
  billing_city: AnyCity
  billing_state: TX
  billing_zipcode: 12345
  billing_country: US
  billing_email_address: "[email protected]"
  billing_phone_number: "5555555555"
  vault_token: vaulttokenvalue

and

beekin:
  name: Beekin
  website: "http://www.beekin.com" 
  hometown_city: Nashville
  hometown_state: TN
  subdomain: beekin
  account_type: monthly
  account_type_name: "MONTHLY"
  account_status: "ACTIVE"
  credit_card_id: 1 // or any other Card id
  next_payment_date: <%= Date.today %>
Bohdan Pohorilets
I'm pretty sure you're not suppose to assign an id to a fixture because you don't know what the id of a certain fixture is going to be when it's inserted into the database.
Koby
I should also add that the account_type monthly refers to the account_types fixture for monthly: and that's what's bothering me. i have them setup almost exactly the same way and the account_type fixture is working as expected while the credit_card one is not.
Koby
I went ahead and tried this and it worked, but I was under the impression that you weren't suppose to hardcode ID values for fixtures, did this change or was this just an exception rather than the rule?
Koby
A: 

This also isn't a great solution, but you can get consistent id's by hashing the name of the fixture with Fixtures.identify.

beekin:
  name: Beekin
  website: "http://www.beekin.com" 
  hometown_city: Nashville
  hometown_state: TN
  subdomain: beekin
  account_type: monthly
  account_type_name: "MONTHLY"
  account_status: "ACTIVE"
  credit_card_id: <%= Fixtures.identify(:card_one) %>
  next_payment_date: <%= Date.today %>

see http://ar.rubyonrails.org/classes/Fixtures.html#M000009 for more details.

(secretly, I think this is how Rails implements the cross file "plain name" references.)

mkirk