Skip to content

Instantly share code, notes, and snippets.

@Canx
Created July 1, 2011 12:38
Show Gist options
  • Save Canx/1058457 to your computer and use it in GitHub Desktop.
Save Canx/1058457 to your computer and use it in GitHub Desktop.
modelos_1
lennon = Persona.find_by_nombre("John Lennon")
lennon.grupo = Grupo.find_by_nombre("Ramones")
lennon.save
class Grupo < ActiveRecord::Base
end
class Persona < ActiveRecord::Base
end
class AddGrupoIdToPersona < ActiveRecord::Migration
def change
add_column :personas, :grupo_id, :integer
add_index :personas, :grupo_id
end
end
class CreateGrupos < ActiveRecord::Migration
def change
create_table :grupos do |t|
t.string :nombre
t.timestamps
end
end
end
class CreatePersonas < ActiveRecord::Migration
def change
create_table :personas do |t|
t.string :nombre
t.timestamps
end
end
end
class Grupo < ActiveRecord::Base
has_many :personas
end
class Persona < ActiveRecord::Base
belongs_to :grupo
end
ActiveRecord::Schema.define(:version => 20110701083257) do
create_table "grupos", :force => true do |t|
t.string "nombre"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "personas", :force => true do |t|
t.string "nombre"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "grupo_id"
end
add_index "personas", ["grupo_id"], :name => "index_personas_on_grupo_id"
end
["Ramones","The Beatles", "The Rolling Stones"].each do |grupo|
Grupo.find_or_create_by_nombre(grupo)
end
joey = Persona.find_or_create_by_nombre('Joey Ramone')
joey.grupo = Grupo.find_by_nombre("Ramones")
joey.save
lennon = Persona.find_or_create_by_nombre('John Lennon')
lennon.grupo = Grupo.find_by_nombre("The Beatles")
lennon.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment