Guest User

Untitled

a guest
Oct 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. ### Polymorphism in Rails
  2. ---
  3. * Allows you to have a model **associated with more than one other model without the use of a join table**
  4. * How?
  5. * Adding a **type** and **id** field to the table of the model you wish to make polymorphic
  6. * When creating the association using `rails g` and appending **name:references{polymorphic}**
  7. can be used to quickly establish the `_type` and `_id` polymorphic column names (all done by Rails).
  8. ---
  9. #### Example: Person, Business & Interaction
  10. * Interactive will belong to an Interaction which Person and Business will have many of:
  11. ```ruby
  12. rails g model Person first_name last_name
  13. rails g model Business name
  14. rails g model Interaction description interactive:references{polymorphic}
  15. # you can use any name for the polymorphic id and type, just as long as they make sense
  16.  
  17. rake db:migrate
  18. ```
  19. * Add to Person and Business class (keep Interaction class the same)
  20. ```ruby
  21. class Person < ApplicationRecord
  22. has_many :interactions, as: :interactive
  23. end
  24.  
  25. class Business < ApplicationRecord
  26. has_many :interactions, as: :interactive
  27. end
  28.  
  29. # This should defaulted by rails
  30. class Interaction < ApplicationRecord
  31. belongs_to :interactive, polymorphic: true
  32. end
  33. ```
  34. * If you open `rails c` and then entered `Interaction.column_names`, you will see:
  35. * `interaction_id`: holds the **id** of the model that the interaction belongs to
  36. * `interaction_type`: holds the **type** of the model that the interaction belongs to
  37. * Both tell Rails which model this interaction should be associated with
  38. * Adding the following controllers:
  39. ```ruby
  40. # in terminal
  41. rails g controller People index show new create edit update
  42. rails g controller Businesses index show new create edit update
  43. rails g controller Interactions new create edit update
  44. ```
  45.  
  46. * **Nest your resources** such that the polymorphic association is nested within each model!
  47. * You will need some way to **parse** which object the polymorphic association belongs to:
  48.    * Checking the **params** for the object's class and then **returning** the object in the db
  49. ```ruby
  50. private
  51. def context
  52. if params[:person_id]
  53. id = params[:person_id]
  54. Person.find(params[:person_id])
  55. else
  56. id = params[:business_id]
  57. Business.find(params[:business_id])
  58. end
  59. end
  60. ```
  61. * For your forms that use the polymorphic association:
  62. ```ruby
  63. <%= form_for [obj1, obj2] do %>
  64. #...
  65. <% end %>
  66. ```
  67. * `obj1` is the object returned from parsing the polymorphic association (ex. @context = context)
  68. * `obj2` is the object of the polymorphic class (ex. Interaction.new)
Add Comment
Please, Sign In to add comment