Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Bootcamp [Sesión 03]

Rails Bootcamp [Sesión 03]

Transcript

  1. RAILS BOOTCAMP 3 JUNIO 2016

  2. ACTIVERECORD

  3. Modelos

  4. $ rails generate model Person

  5. /app/models/person.rb

  6. Migraciones

  7. /db/migrate

  8. $ rake db:migrate

  9. Crear modelo de datos de tu app recuerden migrar los

    nuevos modelos
  10. Jugando con la consola de rails recuerden migrar los nuevos

    modelos
  11. $ rails console

  12. p = Person.first

  13. p.name

  14. Person.all

  15. Crear asociaciones

  16. $ rails generate migration AddReferencesToBook person:references

  17. app/models/person.rb class Person < ActiveRecord::Base has_many :books # has_one :book

    end
  18. app/models/book.rb class Book < ActiveRecord::Base belongs_to :person end

  19. Crear validaciones

  20. app/models/person.rb class Person < ActiveRecord::Base validates :name, presence: true end

  21. Callbacks

  22. before_save after_save before_create after_create before_validation after_validation

  23. app/models/person.rb class Person < ActiveRecord::Base before_save :count_pets def count_pets if

    self.email.nil? puts "No tiene mail" else puts "Yeih! #{self.email}" end end end