rails select only 2 columns error - mysql

script/console output:
>> User.find(:first, :select => '`email`, `pass`, `login`, `id`')
=> #<User pass: "e10adc3949ba59abbe56e057f20f883e", email: "fakemail#bla-bla-bla.com">
>> User.find(:first, :select => '`pass`, `login`, `id`')
=> #<User login: "ostap", pass: "e10adc3949ba59abbe56e057f20f883e">
how i can fix it?
Unix hosting, Ruby 1.8.7, Rails 2.3.5, MySQL 5.1.46
in mysql console all fine...

Try changing ":select => 'pass, login, id'" to :select => 'email, pass, login, id'; i.e. remove the backticks around the fields. I think the backticks in your argument are throwing off the select. Here is the API dock for the find command that lead me to that conclusion.

Related

Ruby connect to mysql using login path

I'm trying to create a connection to mysql from ruby script using login path.
I've set the login path in the following way:
mysql_config_editor set --login-path=login-path --host=host-name --user=user --password
Now in my ruby script I have the following line:
require 'mysql2'
$mysql_connection = Mysql2::Client.new(:default_file => '~/.mylogin.cnf',:default_group => 'login-path')
And I get the following error:
error: Found option without preceding group in config file:
/home/user/.mylogin.cnf at line: 3 Fatal error in defaults handling.
Program aborted
When I'm connecting like this, it succeed:
require 'mysql2'
$mysql_connection = Mysql2::Client.new(:host => 'host-name', :username => "user", :password => "password", :database => 'database_name')
What am I doing wrong?
I think the problem is with your 'default_group' setting. I have standard mysql setup using a typical login-path of 'client', eg:
% mysql_config_editor print
[client]
password = *****
and the following connect works with no issue:
#client = Mysql2::Client.new(host: MySQL_Host, username: MySQL_User, default_group: 'client', database: MySQL_Database)

Ruby Active Record Mysql Error

It seems like there are alot of tutorials online for using ruby to interact with mysql. But none of them work for me. Do I not have something installed correctly? Could someone tell me what I am doing wrong? I will try to put down as much relevant information as I can. I am relatively new to ruby (6 months).
This is my Error:
dyld: lazy symbol binding failed: Symbol not found: _mysql_get_client_info
Referenced from: /Library/Ruby/Gems/2.0.0/gems/mysql-2.9.1/lib/mysql/mysql_api.bundle
Expected in: flat namespace
dyld: Symbol not found: _mysql_get_client_info
Referenced from: /Library/Ruby/Gems/2.0.0/gems/mysql-2.9.1/lib/mysql/mysql_api.bundle
Expected in: flat namespace
Trace/BPT trap: 5
This is my sample script:
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter=> "mysql",
:host => "localhost",
:database=> "#####",
:username => "#####",
:password => "#####"
)
class Rubyist < ActiveRecord::Base
end
Rubyist.create(:name => 'Luc Juggery', :city => "Nashville, Tenessee")
Rubyist.create(:name => 'Sunil Kelkar', :city => "Pune, India")
Rubyist.create(:name => 'Adam Smith', :city => "San Fransisco, USA")
I have the tables created in mysql. I am using the correct username, host, password, and database name. I have a table names Rubyist that has city and name columns.
gem list shows I have activerecord 4.1.1, mysql 2.9.1, mysql2 0.3.16
I have ruby installed ruby -v shows: ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]
I am using a Mac

Dynamic second database Ruby

I want to add a second read-only database to my application based on a variable aside the SQLite file I'm developing with.
So, there should be three databases.
1. Local SQLite file
2. Production Read-only MySQL database
3. Test Read-only MySQL database
This test database has new columns added and will be pushed as production database soon.
From this SO-Q, I do not understand what the solution is.
I already use establish_connection.
establish_connection(
:adapter => 'mysql2',
:database => "db1",
:username => "username",
:password => "p*ssw*rd",
:host => "ho.st.com"
)
This db1 is the production db without the extra table columns.
db2 is the test db with the extra table columns.
establish_connection(
:adapter => 'mysql2',
:database => "db2",
:username => "username",
:password => "p*ssw*rd",
:host => "ho.st.com"
)
I thought this could be a solution:
In the models of the tables from the MySQL databases:
config = 'test'
case config
when 'test'
establish_connection(
:adapter => 'mysql2',
:database => "db1",
:username => "username",
:password => "p*ssw*rd",
:host => "ho.st.com"
)
else
establish_connection(
:adapter => 'mysql2',
:database => "db2",
:username => "username",
:password => "p*ssw*rd",
:host => "ho.st.com"
)
end
But now I have to edit all 'affected' models when I change the status. Is there a way to variabilise (is that even a word?) this so I only have to edit this config once?
I've tried via Application_controller.rb, but this didn't work.
# Application_controller.rb
def get_config_status
return 'test'
end
# model.rb
config = get_config_status() #=> undefined method `get_config_status' for Regio(Table doesn't exist):Class
# or
config = Application.get_config_status #=> uninitialized constant Regio::Application
Or should I consider totally something else?
To be honest, I don't like putting usernames and passwords in Models. Is this a possible thread for hackers?
Summary
What I'm trying to accomplish is:
Set one variable so my application uses db2 instead of db1.
The octopus gem might be helpful.
https://github.com/tchandy/octopus
You can select the database at query level,
User.where(name: 'Sam').using(:db2)
Or you can select the database per controller action,
class ApplicationController < ActionController::Base
around_filter :select_shard
def select_shard(&block)
Octopus.using(:db1, &block)
end
end
Check out the readme on Github for more examples.

Ruby + MySQL error connecting to database on localhost

I'm dealing with this a few days, and cannot connect to a simple mysql database on localhost.
require "mysql"
#db_host = "localhost"
#db_user = "myrubyapp"
#db_pass = "1234"
#db_name = "myrubyapp"
mysql = Mysql.new(:host => #db_host, :username => #db_user, :password => #db_pass, :database => #db_name)
The output I got is an error: can't convert Hash into String (TypeError) where Mysql.new is.
The second one, I tried to change the gem to mysql2
require "mysql2"
#db_host = "localhost"
#db_user = "myrubyapp"
#db_pass = "1234"
#db_name = "myrubyapp"
mysql = Mysql2.new(:host => #db_host, :username => #db_user, :password => #db_pass, :database => #db_name)
The output is an error too, but is different from the first: undefined method "new" for Mysql2:Module (NoMethodError).
Guys I'm sorry that I have to ask this kind of questions, but I'm really really confused, I have an experience of programming more than 3 years in JavaSE and EE, I am ashamed cause I can't deal with that. Please point me into right direction and don't judge me harshly. I am new to Ruby.
Try
mysql = Mysql2::Client.new(:host => #db_host, :username => #db_user, :password => #db_pass, :database => #db_name)
correct syntax is:
client = Mysql2::Client.new(:host => "localhost", :username => "root")
see mysql2 on github for more examples
I recomment you to take a look at Sequel as raw mysql2 lib provide a very limited functionality and Sequel can do a lot.
Never used the gem mysql, but have you tried to remove the hash :host => etc and pass directly a list of parameters?
Something like con = Mysql.new db_host, db_user, db_pass, db_name
http://zetcode.com/db/mysqlrubytutorial/ for a tutorial

exception_notification: smtp not working

I am using Rails 3.2.5 and exception_notification gem. In production mode, I am generally sending emails using PostMarkApp's postmark-rails gem.
Initially, I got a View error from exception_notification gem stating
ActionView::Template::Error (code converter not found (UTF-8 to UTF-16))
so based on exception_notification gem raises ActionView::Template::Error (code converter not found (UTF-8 to UTF-16)) only on Heroku production mode, I moved to
gem 'exception_notification', git: 'git://github.com/alanjds/exception_notification.git'
This solved that bug. Now, I want the gem to send emails from my gmail account instead of using PostMarkApp credits, so I added the following to my production.rb, yet Exception Notification attempts to send email only from Post Mark App. Why is this setting not working?
config.middleware.use ExceptionNotifier,
sender_address: 'noreply#mydomain.com',
exception_recipients: 'myemail#mydomain.com',
sections: %w{current_user} + ExceptionNotifier::Notifier.default_sections,
ignore_crawlers: %w{Googlebot bingbot},
email_format: true,
normalize_subject: true,
smtp_settings: {
:address => "smtp.gmail.com",
:port => "587",
:domain => "www.gmail.com",
:user_name => "myemail#gmail.com",
:password => "mypassword",
:authentication => "plain",
:enable_starttls_auto => true,
:openssl_verify_mode => 'none'
}
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => "_____" }
For some reason it appears that SMTP delivery does NOT work in the development environment. I tried a number of different settings and was never able to get this to work. It DOES however work in my other environments. Older posts seem to indicate this as well:
http://www.ruby-forum.com/topic/64776
http://www.devchix.com/2008/12/09/how-to-see-exception_notification-plugin-work-in-development-mode/
In development, I am using the following in my development.rb:
config.action_mailer.delivery_method = :letter_opener
config.middleware.use ExceptionNotifier,
:sender_address => 'test#test.com',
:exception_recipients => 'recipient#test.com'
In my "staging" environment, I am using the following in my staging.rb:
config.action_mailer.delivery_method = :smtp
config.middleware.use ExceptionNotifier,
:sender_address => 'test#test.com',
:exception_recipients => 'recipient#test.com'
The staging.rb acquires it's SMTP settings from an initializer I have the uses SendGrid for SMTP:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 25,
:domain => "test.com",
:user_name => "user_name",
:password => "password",
:authentication => "plain"
}
Try the suggestion at http://www.scottw.com/multiple-smtp-servers-with-action-mailer
or at Rails ActionMailer with multiple SMTP servers