user1703579
user1703579

Reputation: 11

Can't create database object in Rails

I'm creating a simple expense table and none of the ActiveRecord methods seem to work for creating a data object and passing it parameters. I've done the same thing before without any problems and it works for an existing table, so I must be missing something simple (I'm a newbie).

Here's what I did: 1) Created a migration:

rails generate migration create_expenses

2) Modified the migration:

class CreateExpenses < ActiveRecord::Migration
  def up
    create_table 'expenses' do |t|
      t.string :description
      t.date :date
      t.timestamps
    end
  end
  def down
    drop_table 'expenses'
  end
end

3) Migrate

rake db:migrate

4) Create Model

class Expense < ActiveRecord::Base
end

5) Test and try to create an expense

testexp= Expense.create!(description: "Test Expense", date: "9/27/12")
 SQL (1.7ms) INSTERT INTO "expenses" ("created_at", "date", "description", "updated_at") VALUES (?,?,?,?) [["created_at", Thu, 27 Sep 2012 13:50:47 UTC +00:00],["date", nil], ["description", nil], ["updated_at", Thu, 27 Sep 2012 13:50:47 UTC +00:00]]
=> #<Expense id: 1, description:nil, date:nil, created_at: "2012-09-27 13:50:47", updated_at: "2012-09-27 13:50:47">

*I've tried all different forms of hashing, create, new, etc. None seem to work. After I create, I can change the values by testexp.description = "Test Expense", testexp.save.

Any help would be greatly appreciated!

Upvotes: 1

Views: 602

Answers (1)

Yarneo
Yarneo

Reputation: 3000

It seems your database does exist and is working find, hence the new created record.
I think what might be your problem is when you do the create you don't add the parameters properly, it should actually look like this:

Expense.new(:description => "Test Expense", :date => "9/27/12")

Upvotes: 1

Related Questions