Advertisement
Guest User

Untitled

a guest
May 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. ### problems
  2. ```ruby
  3. class Address < ActiveRecord::Base
  4. belongs_to :customer
  5. end
  6.  
  7. class Customer <ActiveRecord::Base
  8. has_one :address
  9. has_many :invoices
  10. end
  11.  
  12. class Invoice < ActiveRecord::Base
  13. belongs_to :customer
  14. end
  15. ```
  16.  
  17. ```html
  18. <%= @invoice.customer.name %>
  19. <%= @invoice.customer.address.city %>
  20. <%= @invoice.customer.address.street %>
  21. <%= @invoice.customer.address.state %>
  22. ```
  23. ### solutions
  24. 1. use only one dot
  25. ```ruby
  26. class Address < ActiveRecord::Base
  27. belongs_to :customer
  28. end
  29.  
  30. class Customer < ActiveRecord::Base
  31. has_one :address
  32. has_many :invoices
  33.  
  34. def street
  35. address.street
  36. end
  37.  
  38. def city
  39. address.city
  40. end
  41. end
  42.  
  43. class Invoice < ActiveRecord::Base
  44. belongs_to :customer
  45.  
  46. def customer_name
  47. customer.name
  48. end
  49.  
  50. def customer_street
  51. customer.street
  52. end
  53.  
  54. def customer_city
  55. customer.city
  56. end
  57. end
  58. ```
  59.  
  60. ```html
  61. <%= @invoice.customer_name %>
  62. <%= @invoice.customer_city %>
  63. <%= @invoice.customer_street %>
  64. ```
  65.  
  66. > tooo many methods
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement