isnt actually updating the movie record for Inception at all its setting the

Isnt actually updating the movie record for inception

This preview shows page 188 - 190 out of 517 pages.

isn’t actually updating the movie record for Inception at all: it’s setting the movie_id field of both Alice’s and Bob’s reviews to “link” them to Inception . Of course, if we had actually modified any of Inception ’s attributes, inception.save! would try to persist them; but because save! is transactional—that is, it’s all-or-nothing—if the save! fails then every aspect of it fails, so neither the changes to Inception nor its associated Reviews would be saved. m.reviews returns an Enumerable of all owned reviews m.reviews=[r1,r2] Replaces the set of owned reviews with the set r1,r2 , adding or deleting as appropriate, by setting the movie_id field of each of r1 and r2 to m.id ( m ’s primary key) in the database immediately. m.reviews<<r1 Adds r1 to the set of m ’s reviews by setting r1 ’s movie_id field to m.id . The change is written to the database immediately (you don’t need to do a separate save ). r = m.reviews.build(:potatoes=>5) Makes r a new, unsaved Review object whose movie_id is preset to indicate that it belongs to m . Arguments are the same as for Review.new . r = m.reviews.create(:potatoes=>5) Like build but saves the object immediately (analogous to the difference between new and save ). Note: if the parent object m has never been saved, that is, m.new_record? is true, then the child objects aren’t saved until the parent is saved. m = r.movie Returns the Movie instance associated with this review r.movie = m Sets m as the movie associated with review r Figure 5.15: A subset of the association methods created by movie has_many :reviews and review belongs_to :movie , assuming m is an existing Movie object and r1,r2 are Review objects. Consult the ActiveRecord::Associations documentation for a full list. Method names of association methods follow convention over configuration based on the name of the associated model. Figure 5.15 lists some of the most useful methods added to a movie object by virtue of declaring that it has_many reviews. Of particular interest is that since has_many implies a collection of the owned object (Reviews), the reviews method quacks like a collection. That is, you can use all the collection idioms of Figure 3.7 on it—iterate over its elements with each , use functional idioms like sort , search and map , and so on, as in lines 8, 11 and 14 of Figure 5.13 . What about the belongs_to method calls in review.rb ? As you might guess, belongs_to :movie gives Review objects a movie instance method that looks up and returns the movie to which this review belongs. Since a review belongs to at most one movie, the method name is singular rather than plural, and returns a single object rather than an enumerable.
Image of page 188
has_one is a close relative of has_many that singularizes the association method name and operates on a single owned object rather than a collection.
Image of page 189
Image of page 190

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