Rails

1,756 views

Published on

Published in: Business
  • Be the first to comment

Rails

  1. 1. Rails (1) <ul><li>Modelling the data </li></ul>Raymond van Dongelen | NHL | www.webxy.nl
  2. 2. MVC <ul><li>Model-view-controller (MVC) is a software architecture that separates an application's data model, user interface, and control logic into three distinct components so that modifications to one component can be made with minimal impact to the others. </li></ul><ul><li>Seperate data from decisions from view </li></ul><ul><li>Create maintainable applications </li></ul><ul><li>Let each layer perform tasks it is best at </li></ul>
  3. 3. Model <ul><li>The data that is needed for the application </li></ul><ul><li>The data is transient (is saved between sessions) </li></ul><ul><li>Closely related to databases </li></ul>
  4. 4. Controller <ul><li>Makes important decisions </li></ul><ul><li>Controls traffic </li></ul>
  5. 5. View <ul><li>Displays information </li></ul><ul><li>Handles small events </li></ul>
  6. 6. Scaffolding <ul><li>Ruby generates standard functionality for your application, including a models, views and controllers </li></ul><ul><li>You adapt it </li></ul>
  7. 7. Datamodel <ul><li>Should prevent storing the same information twice </li></ul><ul><li>Should guard relations in the data </li></ul><ul><li>Should be able to cope with changing data structure </li></ul>
  8. 8. Ruby and data <ul><li>All database handling is done in the model </li></ul><ul><li>Powerful features to validate data, guard relations, search data. </li></ul><ul><li>All data is wrapped in Ruby classes </li></ul><ul><li>SQL is almost never needed </li></ul>
  9. 9. Ruby naming <ul><li>Table name plural </li></ul><ul><li>Key in a table “id” </li></ul><ul><li>Reference to another table “table_id” </li></ul>
  10. 10. Relations <ul><li>belongs_to </li></ul><ul><li>has_one </li></ul><ul><li>has_and_belongs_to_many </li></ul><ul><li>has_many </li></ul>
  11. 11. Let’s rock <ul><li>Like we never done before... </li></ul>
  12. 12. ActiveRecord(1) <ul><li>Validators: </li></ul><ul><li>validates_presence_of </li></ul><ul><li>validates_uniqueness_of </li></ul><ul><li>Relations: </li></ul><ul><li>belongs_to </li></ul><ul><li>has_one </li></ul><ul><li>has_and_belongs_to_many </li></ul><ul><li>has_many </li></ul>
  13. 13. ActiveRecord(2) <ul><li>Example: </li></ul><ul><li>class Agenda < ActiveRecord::Base </li></ul><ul><li>belongs_to :user </li></ul><ul><li>validates_presence_of :user, :message => &quot;cannot be empty&quot; </li></ul><ul><li>end </li></ul>
  14. 14. ActiveRecord (3) <ul><li>@agenda = Agenda.find(1) </li></ul><ul><li>puts (@agenda.user.name) </li></ul><ul><li><== We are able to display the user, it is automatically fetched </li></ul>
  15. 15. ActiveRecord (4) <ul><li>Dynamic find </li></ul><ul><li>Agenda.find_all_name (“Home”) </li></ul><ul><li>User.find_all_by_email (“ [email_address] ”) </li></ul>
  16. 16. ActiveRecord (5) <ul><li>Many many powerful features </li></ul><ul><li>Read chapter 14 agile web development with Ruby on Rails </li></ul>
  17. 17. Example db

×