To get connected you database from your ruby file just include the "ActiveRecord" inside your ruby file.
require 'rubygems'
require 'activerecord'
#establish the connection.
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:database => "database",
:username => "username",
:password => "password",
:host => "localhost"
)
#create a class inherit with you activerecord base class.
class User < ActiveRecord::Base
end
user = User.find(:first)
puts user
Or
class SomeThing < ActiveRecord::Base
set_table_name :users
end
user = SomeThing.find(:first)
puts user
That's It.
Monday, September 28, 2009
Ruby Mysql database connectivity
In this section I am going to tell you how do you get connected your MySQL database using Ruby.
Make sure that you have install mysql adapter
To verify you mysql file go to command prompt type irb -> require 'mysql' if it's return true that means the file is loaded.
Now use your fav editor let say scite.
require 'rubygems'
require 'mysql'
begin
#establish a connection.
mysql = Mysql.real_connect("hostname(e.g - localhost)", "username", "password", "databasename")
result = mysql.query("SELECT * FROM USERS;")
result.each do |row|
row.each do |col|
puts col
end
end
#the col object will return the value from the users table.
# if you want to insert a record. then
mysql.query("INSERT INTO users (email) VALUES ('example@example.com')")
#this is how you can edit delete the records...
rescue Exception => e
puts "I guess something went wrong. #{e}"
ensure
# clone the connection...
mysql.close if mysql
end
Make sure that you have install mysql adapter
To verify you mysql file go to command prompt type irb -> require 'mysql' if it's return true that means the file is loaded.
Now use your fav editor let say scite.
require 'rubygems'
require 'mysql'
begin
#establish a connection.
mysql = Mysql.real_connect("hostname(e.g - localhost)", "username", "password", "databasename")
result = mysql.query("SELECT * FROM USERS;")
result.each do |row|
row.each do |col|
puts col
end
end
#the col object will return the value from the users table.
# if you want to insert a record. then
mysql.query("INSERT INTO users (email) VALUES ('example@example.com')")
#this is how you can edit delete the records...
rescue Exception => e
puts "I guess something went wrong. #{e}"
ensure
# clone the connection...
mysql.close if mysql
end
Friday, September 11, 2009
bash: cap: command not found
bash: cap: command not found
In this post I'll be talking about how to resolve the “bash: cap: command not found” issue. Even after installing Capistrano we get the following error “bash: cap: command not found” the reason behind this is that the path is not decalred in bash file, so to fix the problem you have to add the following line on code inside bashrc file
Step 1
#go to Terminal
#type
sudo gedit ~/.bashrc
Step 2
#Add the following line at the bottom of the file.
export PATH=$PATH:/home/user/.gem/ruby/1.8
#save and close
# To find the installation of you gem just run the following command
#gem list -d c
# capistrano (2.5.8)
# Authors: Jamis Buck, Lee Hambley
# Rubyforge: http://rubyforge.org/projects/capistrano
# Homepage: http://www.capify.org
# Installed at: /home/user/.gem/ruby/1.8
Step 3
#From command prompt
source ~/.bashrc
In this post I'll be talking about how to resolve the “bash: cap: command not found” issue. Even after installing Capistrano we get the following error “bash: cap: command not found” the reason behind this is that the path is not decalred in bash file, so to fix the problem you have to add the following line on code inside bashrc file
Step 1
#go to Terminal
#type
sudo gedit ~/.bashrc
Step 2
#Add the following line at the bottom of the file.
export PATH=$PATH:/home/user/.gem/ruby/1.8
#save and close
# To find the installation of you gem just run the following command
#gem list -d c
# capistrano (2.5.8)
# Authors: Jamis Buck, Lee Hambley
# Rubyforge: http://rubyforge.org/projects/capistrano
# Homepage: http://www.capify.org
# Installed at: /home/user/.gem/ruby/1.8
Step 3
#From command prompt
source ~/.bashrc
Wednesday, September 9, 2009
File Upload using AJAX and jQuery.
File Upload using AJAX and jQuery.
File upload using jQuery is preety simple.
NOTE: please include the jQuery Library afer prototype.js
As you can in the above example I had declared “jQuery.noConflict();” the reason behind this is to abvoid the conflict between jQuery library and prototype.js.
<% remote_form_for :some_random_name, @some_object, :url => {:action => 'testtest'},
:html => { :multipart => true, :id => "upload_form" } do |f| %>
<%= file_field_tag "test" "uploaded" %>
< id="button" class="button" value="Add File" type="button">
< id="save" class="save" value="Upload" type="submit">
<%end%>
Thats it.
File upload using jQuery is preety simple.
NOTE: please include the jQuery Library afer prototype.js
As you can in the above example I had declared “jQuery.noConflict();” the reason behind this is to abvoid the conflict between jQuery library and prototype.js.
<% remote_form_for :some_random_name, @some_object, :url => {:action => 'testtest'},
:html => { :multipart => true, :id => "upload_form" } do |f| %>
<%= file_field_tag "test" "uploaded" %>
< id="button" class="button" value="Add File" type="button">
< id="save" class="save" value="Upload" type="submit">
<%end%>
Thats it.
Subscribe to:
Posts (Atom)