dimanche 26 juin 2016

Ruby, MySQL, and ActiveRecord

Im at wits end. I am trying to create a simple Ruby app that could query and append a database with three different tables, users, postings, and offers.

To get test out this strategy, I created a simple script to write to the users database.
lib/NegotiGate_DB.rb

require 'active_record'

module NegotiGateDB

  client = ActiveRecord::Base.establish_connection(
    :host => 'localhost', 
    :user => 'andrewgy8', 
    :database => './negotiation',
    :adapter => 'mysql'
    )

  class User < ActiveRecord::Base
  end

  User.create(f_name: 'Andrew', l_name: 'Smith' )

  seller = User.find(:first)

  puts "#{seller} is the first user."

end

In terminal I created the DB Negotiation.

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| negotiation        |
| performance_schema |
| ruby               |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

mysql> use negotiation;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------------+
| Tables_in_negotiation |
+-----------------------+
| offers                |
| postings              |
| users                 |
+-----------------------+
3 rows in set (0.00 sec)

mysql> describe users;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id         | int(11)      | NO   | PRI | NULL    |       |
| f_name     | varchar(255) | YES  |     | NULL    |       |
| l_name     | varchar(255) | YES  |     | NULL    |       |
| email      | varchar(255) | YES  |     | NULL    |       |
| phone      | int(11)      | YES  |     | NULL    |       |
| pword_hash | varchar(255) | YES  |     | NULL    |       |
| role       | varchar(255) | YES  |     | NULL    |       |
| created    | datetime     | YES  |     | NULL    |       |
| updated    | datetime     | YES  |     | NULL    |       |
| enabled    | tinyint(1)   | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+

10 rows in set (0.00 sec)

When I run ruby NegotiGate_DB.rb I get this error:

/Users/andrewgraham-yooll/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.2/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'mysql' for database adapter, but the gem is not loaded. Add `gem 'mysql'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
from /Users/andrewgraham-yooll/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.2/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
from /Users/andrewgraham-yooll/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.2/lib/active_record/connection_handling.rb:50:in `establish_connection'
from NegotiGate_DB.rb:5:in `<module:NegotiGateDB>'
from NegotiGate_DB.rb:3:in `<main>'

Here is my Gemfile:

source 'https://rubygems.org'

# Specify your gem's dependencies in NegotiGate.gemspec
gem 'spec'
gem 'mysql'
gem 'activerecord'

Im at a loss...

I have tried the solutions on SO reccomending verions changes, but nothing seems to work. I have also tried using SQLite3 and Mysql2 with the same error message showing up. If I delete the adapter parameter for establish_connection I get an error message:

/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.2/lib/active_record/connection_adapters/connection_specification.rb:171:in `spec': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
from /Users/andrewgraham-yooll/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.2/lib/active_record/connection_handling.rb:50:in `establish_connection'
from NegotiGate_DB.rb:5:in `<module:NegotiGateDB>'
from NegotiGate_DB.rb:3:in `<main>'

I hope this is an easy fix because I think it would be a ton of fun to work with databases in a language like ruby.

Perhaps its not as feasible as I thought it would be. If you don't think it is, I would appreciate any recommendations. Thanks so much!

Aucun commentaire:

Enregistrer un commentaire