Bobby Tables
Bobby Tables

Reputation: 1184

ActiveRecord Won't Update

My simple Sqlite3 database is as follows:

CREATE TABLE balances(
balance zilch
);

My Ruby is as follows:

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

When I exit, and come back, and enter in the same Ruby again, at first, (before I runx.balance = 50) balance is nil. Why is this? Why isn't my DB saving?

Upvotes: 1

Views: 104

Answers (2)

Douglas G. Allen
Douglas G. Allen

Reputation: 2261

This is an old demo way of using Active Record and not very useful for production. It will get you started though. My code will make connections without sqlite3 gem required. I think that Active Record will include it if you use the :adapter hash entry. Of course you need it installed but it's not really needed in your code for Active Record. Just try it without that require to see. Then if you're still in doubt, un-install the gem just for fun. There are more Active Record namespaces and methods you should try especially ones that check to see if the database already exists. Then by pass the creation of one. Here's some sample code from the book Metaprogramming 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'

This code deletes the db file after usage and that's not very good persistence either. But you get the idea as it's just for testing assertions. You could try that to be sure as you change balances.

Do you want the rest of the code? https://pragprog.com/book/ppmetr/metaprogramming-ruby

Am I training you or am I the like? Moderators delete this if I'm wrong here please. I don't want to set a bad example.

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230521

If you enter the same code, then you're creating a new object again. No wonder its balance is nil.

To check that your object is saved, you can (for example) check Balance.count before and after record creation.

Upvotes: 2

Related Questions