Posted By

chrisaiv on 10/27/08


Tagged

rails ruby


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

aka
webstic
octoberborn
kukhtenko
tof
pedro84


Rails: Basics


 / Published in: Rails
 

Basic Rails commands for newbies

  1. ####################################
  2. #Models
  3. ####################################
  4.  
  5. #Create a Model
  6. $ ruby script/generate model ClassName attribute_name:attribute_type attribute_name:attribute_type
  7.  
  8. ####################################
  9. #Controllers
  10. ####################################
  11.  
  12. #Create a Controller
  13. $ ruby script/generate controller ClassName action1 action2
  14.  
  15. #Remove a Controller
  16. $ ruby script/destroy controller ClassName action1 action2
  17.  
  18. ####################################
  19. #Server
  20. ####################################
  21.  
  22. #Start an application
  23. $ script/server
  24.  
  25. ####################################
  26. #Migrations
  27. ####################################
  28. #Migrate does these 3 things
  29. #1. Checks the database for the unique number of the migration is most recent
  30. #2. It steps through the migrations that have not yet been applied, one at a time.
  31. #3. The Self.up method is executed for each migration
  32. $ rake db:migrate
  33.  
  34. #Rollback a Migration
  35. $ rake db:migrate VERSION=n
  36.  
  37. #Retrieving Records by ID, not recommended but at times helpful
  38. ModelName = (Most likely TableName)
  39. IRB>> ModelName.find(#id)
  40. IRB>> ModelName.find(:all)
  41.  
  42. #Retrive the Last Record
  43. IRB>> ModelName.find(:all).last (Resource Intensive)
  44. IRB>> ModelName.find(:first, :order => 'id DESC') (Resource Efficient)

Report this snippet  

You need to login to post a comment.