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).
我正在创建一个简单的费用表,并且没有一个ActiveRecord方法似乎适用于创建数据对象并传递它的参数。我之前做过同样的事情没有任何问题,它适用于现有的桌子,所以我必须缺少一些简单的东西(我是新手)。
Here's what I did: 1) Created a migration:
这就是我做的:1)创建了一个迁移:
rails generate migration create_expenses
2) Modified the migration:
2)修改了迁移:
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
3)迁移
rake db:migrate
4) Create Model
4)创建模型
class Expense < ActiveRecord::Base
end
5) Test and try to create an expense
5)测试并尝试创建费用
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.
*我尝试了所有不同形式的散列,创建,新等等。似乎无效。创建后,我可以通过testexp.description =“Test Expense”,testexp.save更改值。
Any help would be greatly appreciated!
任何帮助将不胜感激!
1 个解决方案
#1
1
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")
#1
1
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")