Skip to content

Instantly share code, notes, and snippets.

@rodrei
Created April 10, 2013 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrei/5354738 to your computer and use it in GitHub Desktop.
Save rodrei/5354738 to your computer and use it in GitHub Desktop.
ActiveRecord example
require "rubygems"
require "active_record"
#Change this to reflect your database settings
ActiveRecord::Base.establish_connection (
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "password",
:database => "some_database")
#Now define your classes from the database as always
class User < ActiveRecord::Base
#Si la tabla se llama "users", la va a reconocer automaticamente
#Si la tabla se llama de otra manera:
#set_table_name 'uSeRs'
end
#Recorrer todos los registros de la tabla 'users' e imprimir el campo nombre para cada una:
User.all.each do |user|
puts user.name
end
#Crear un nuevo usuario
User.create(:name => "Rodrigo", :age => 26)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment