The point of these two sections though is not just to explain how to use

The point of these two sections though is not just to

This preview shows page 192 - 194 out of 517 pages.

The point of these two sections, though, is not just to explain how to use associations, but to help you appreciate the elegant use of duck typing and metaprogramming that makes them possible. In Figure 5.14 (c) you added has_many :reviews to the Movie class. The has_many method performs some metaprogramming to define the new instance method reviews= that we used in Figure 5.13 . As you’ve no doubt guessed, convention over configuration determines the name of the new method, the table it will use in the database, and so on. Just like attr_accessor , has_many is not a declaration, but an actual method call that does all of this work at runtime, adding a slew of new model instance methods to help manage the association. 1 class Review < ActiveRecord::Base 2 # review is valid only if it’s associated with a movie: 3 validates :movie_id, :presence => true 4 # can ALSO require that the referenced movie itself be valid 5 # in order for the review to be valid: 6 validates_associated :movie 7 end Figure 5.18: This example validation on an association ensures that a review is only saved if it has been associated with some movie. Associations are one of the most feature-rich aspects of Rails, so take a good look at the full documentation for them. In particular: Just like ActiveRecord lifecycle hooks, associations provide additional hooks that can be triggered when objects are added to or removed from an association (such as when new Reviews are added for a Movie), which are distinct from the lifecycle hooks of Movies or Reviews themselves. Validations can be declared on associated models, as Figure 5.18 shows. Because calling save or save! on an object that uses associations also affects the associated objects, various caveats apply to what happens if any of the saves fails. For example, if you have just created a new Movie and two new Reviews to link to it, and you now try to save the Movie, any of the three saves could fail if the objects aren’t valid (among other reasons). Additional options to association methods control what happens to “owned” objects when an “owning” object is destroyed. For example, has_many :reviews,:dependent=>:destroy specifies that the reviews belonging to a movie should be deleted from the database if the movie is destroyed.
Image of page 192
Through-associations summary: When two models A and B each have a has-one or has-many relationship to a common third model C, a many-to-many association between A and B can be established through C. The :through option to has_many allows you to manipulate either side of a through-association just as if it were a direct association. However, if you modify a through-association directly, the intermediate model object must be automatically created, which is probably not what you intended. ELABORATION: Has and belongs to many Given that has_many :through creates “many-to-many” associations between the two outer entities (Movies and Reviews in our running example), could we create such many-to-many relationships directly, without going through an “intermediate” table?
Image of page 193
Image of page 194

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors