Skip to content

Instantly share code, notes, and snippets.

@Fernan2
Fernan2 / dynamic.rb
Last active February 22, 2017 17:22
user.phone_1 = '963112233'
user.phone_4 = '666111000'
user.phone_2 = '678111000'
phones = (1..4).map do |i|
user.send("phone_#{i}")
end
=> ['963112233', '678111000', nil, '666111000']
@Fernan2
Fernan2 / hash.rb
Last active February 20, 2017 22:23
{
nil => "No se encuentra",
'A' => "Opción A",
'X' => AMOUNT * RATE
}
ORDER_TO_BD = {
'cheap' => 'price',
'expensive' => 'price DESC',
'popular' => 'likes_count DESC'
validates :name, presence: true
def my_method
item.method
if subscriber&.valid?
workdays = %w(lun mar mie jue vie)
validates(:name, { presence: true })
def my_method()
item.method()
if subscriber && subscriber.valid?
workdays = ["lun", "mar", "mie", "jue", "vie"]
for i in 0..(subscribers.length - 1)
subscribers[i].notify
end
for suscriber in subscribers
subscriber.notify
end
suscribers.each do |subscriber|
subscriber.notify
@Fernan2
Fernan2 / prestamo.rb
Created January 17, 2017 19:46
Enumerados en Rails y Postgres 6
class Prestamo < ActiveRecord::Base
enum finalidad: {
reunificar: 'Reunificar deudas',
estudios: 'Estudios',
imprevistos: 'Imprevistos',
compra_vehiculo: 'Compra de vehículo',
viaje: 'Viaje',
reformas: 'Reformas hogar',
otros: 'Otros'
}
@Fernan2
Fernan2 / console.rb
Created January 17, 2017 19:40
Enumerados en Rails y Postgres 5
Prestamo.estudios.count
@prestamo.estudios!
@prestamo.estudios?
@Fernan2
Fernan2 / console.rb
Last active January 17, 2017 19:40
Enumerados en Rails y Postgres 4
Prestamo.where(finalidad: 'Estudios').count
@prestamo.update_attribute(:finalidad, 'Estudios')
@prestamo.finalidad == 'Estudios'
@Fernan2
Fernan2 / 20170118114400_add_finalidad_to_prestamos.rb
Last active January 17, 2017 19:30
Enumerados en Rails y Postgres 3
class AddFinalidadToPrestamos < ActiveRecord::Migration
def change
add_column :prestamos, :finalidad, :finalidad_prestamo
end
end
@Fernan2
Fernan2 / 20170117114400_create_prestamos.rb
Created January 17, 2017 19:23
Enumerados en Rails y Postgres 2
class CreatePrestamos < ActiveRecord::Migration
def change
create_table :prestamos do |t|
...
t.column :finalidad, :finalidad_prestamo
...
end
end
end