Ruby ActiveRecord赢得';t更新
我的简单Sqlite3数据库如下所示:Ruby ActiveRecord赢得';t更新,ruby,database,sqlite,activerecord,ruby-1.9,Ruby,Database,Sqlite,Activerecord,Ruby 1.9,我的简单Sqlite3数据库如下所示: CREATE TABLE balances( balance zilch ); require('active_record') ActiveRecord::Base.establish_connection(:database => "testbalance.db", :adapter => "sqlite3") class Balance < ActiveRecord::Base end x = Balance.new x.bala
CREATE TABLE balances(
balance zilch
);
require('active_record')
ActiveRecord::Base.establish_connection(:database => "testbalance.db", :adapter => "sqlite3")
class Balance < ActiveRecord::Base
end
x = Balance.new
x.balance = 50
x.save
我的建议如下:
CREATE TABLE balances(
balance zilch
);
require('active_record')
ActiveRecord::Base.establish_connection(:database => "testbalance.db", :adapter => "sqlite3")
class Balance < ActiveRecord::Base
end
x = Balance.new
x.balance = 50
x.save
require('active_record')
ActiveRecord::Base.building_连接(:database=>“testbalance.db”,:adapter=>“sqlite3”)
类余额
当我退出并返回,然后再次进入同一个Ruby时,首先(在我运行
x.balance=50
之前),balance是nil
。为什么会这样?为什么我的数据库不保存?如果输入相同的代码,则再次创建新对象。难怪它的余额是零
要检查对象是否已保存,您可以(例如)在创建记录之前和之后检查
余额.计数。这是使用活动记录的一种旧演示方式,对生产不是很有用。这会让你开始的。我的代码将在不需要sqlite3 gem的情况下建立连接。我认为如果使用:adapter散列条目,活动记录将包括它。当然,您需要安装它,但在活动记录的代码中并不真正需要它。只要试一下,不需要看。然后,如果您仍然有疑问,取消安装gem只是为了好玩。有更多的活动记录名称空间和方法您应该尝试,特别是那些检查数据库是否已经存在的方法。然后顺便创造一个。
下面是《元编程Ruby》一书中的一些示例代码
#---
# Excerpted from "Metaprogramming Ruby",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/ppmetr2 for more book information.
#---
# Create a new database each time
File.delete 'dbfile' if File.exist? 'dbfile'
require 'active_record'
ActiveRecord::Base.establish_connection :adapter => "sqlite3",
:database => "dbfile.sqlite3"
# Initialize the database schema
ActiveRecord::Base.connection.create_table :ducks do |t|
t.string :name
end
class Duck < ActiveRecord::Base
validate do
errors.add(:base, "Illegal duck name.") unless name[0] == 'D'
end
end
my_duck = Duck.new
my_duck.name = "Donald"
my_duck.valid? # => true
my_duck.save!
require_relative '../test/assertions'
assert my_duck.valid?
bad_duck = Duck.new(:name => "Ronald")
assert !bad_duck.valid?
duck_from_database = Duck.first
duck_from_database.name # => "Donald"
assert_equals "Donald", duck_from_database.name
duck_from_database.delete
File.delete 'dbfile' if File.exist? 'dbfile'
#---
#摘自“元编程Ruby”,
#由实用书架出版。
#版权适用于本代码。不可用于制作培训材料,
#课程、书籍、文章等。如果您有疑问,请联系我们。
#我们不保证此代码适用于任何用途。
#拜访http://www.pragmaticprogrammer.com/titles/ppmetr2 更多图书信息。
#---
#每次创建一个新数据库
如果File.exist存在,是否删除“dbfile”数据库文件'
需要“活动记录”
ActiveRecord::Base.build\u连接:适配器=>“sqlite3”,
:database=>“dbfile.sqlite3”
#初始化数据库架构
ActiveRecord::Base.connection.create_表:ducks do|t|
t、 字符串:名称
结束
类Duck真的
我的鸭子,救命!
需要相对“../test/assertions”
断言我的鸭子有效吗?
bad_duck=duck.new(:name=>“Ronald”)
断言!坏鸭子,有效吗?
duck\u from_数据库=duck.first
duck_from_database.name#=>“Donald”
assert_等于“Donald”,duck_来自_database.name
从_数据库中删除_。删除
如果File.exist存在,是否删除“dbfile”数据库文件'
这段代码在使用后删除db文件,这也不是很好的持久性。但是你得到了这个想法,因为它只是为了测试断言。你可以尝试一下,以确保你改变平衡
你想要剩下的代码吗
我是在训练你还是像你一样?版主删除这个,如果我在这里错了,请。我不想树立一个坏榜样。JFI是rails应用程序的代码,还是ruby应用程序的代码,只是想知道ruby在哪里需要'sqlite3'可以看到它