Naming Conventions for Ruby on Rails

You’re using Ruby on Rails to create a Web application or Web database app, which is very smart of you. Depending on what you’re working with — an application, a one-to-many relationship, or a many-to-many relationship — you use different variations on Rails naming protocols, which are explained in the following sections.


Ruby naming for new apps


When you create a new application — for example, an album project with a photos database table — use the following steps:



  1. Create a Rails project named album.



  2. Create databases named album_development, album_test, album_production.



  3. Generate a Photo model. (In the RadRails Generators view, select model in the drop-down list, and type Photo in the text field to the right of the drop-down list.)



    • Rails creates a class named Photo in a file named photo.rb.



    • Rails creates a migration file named 001_create_photos.rb.





  4. Create a database table named photos.



  5. Generate a Photo scaffold. (In the RadRails Generators view, select scaffold in the drop-down list and type Photo in the text field to the right of the drop-down list.)



    • Rails creates a class named PhotosController in a file named photos_controller.rb.





  6. Visit http://localhost:300x/photos/.




Ruby naming in a one-to-many relationship


When you work with a foreign key in a one-to-many relationship (for example, one photo with many comments), follow these tips:



  • The comments table has a photo_id column.



  • The Comment model contains the statement belongs_to :photo.



  • The Photo model contains the statement has_many :comments.




Ruby naming in a many-to-many relationship


When you work with a many-to-many relationship (for example, photos and tags), keep these protocols in mind:



  • The Photo model contains the statement has_and_belongs_to_many :tags.



  • The Tag model contains the statement has_and_belongs_to_many :photos.



  • The photos_tags table (so named because photos comes before tags alphabetically) has no id column.






dummies

Source:http://www.dummies.com/how-to/content/naming-conventions-for-ruby-on-rails.html

No comments:

Post a Comment