Self Check 421 In line 3 of Figure 43 how many arguments are we passing to

Self check 421 in line 3 of figure 43 how many

This preview shows page 142 - 144 out of 517 pages.

Self-Check 4.2.1. In line 3 of Figure 4.3 , how many arguments are we passing to create_table , and of what types? Two arguments: the first is a string and the second is a block. We used poetry mode, allowing us to omit parentheses. Self-Check 4.2.2. In Figure 4.3 , the ____ method yields ____ to the block. create_table ; the variable t 4.3 Models: Active Record Basics With our Movies table ready to go, we’ve completed the first three steps—app creation, routing, and initial migration—so it’s time to write some app code. The database stores the model objects, but as we said in Chapter 2 , Rails uses the Active Record design pattern to “connect” models to the database, so that’s what we will explore next. Create a file app/models/movie.rb containing just these three lines: 1 class Movie < ActiveRecord::Base 2 attr_accessible :title, :rating, :description, :release_date 3 end Thanks to convention over configuration, those three lines in movie.rb enable a great deal of behavior. To explore some of it, stop the running application with Control-C and instead run rails console , which gives you an interactive Ruby prompt like irb(main):001.0> with the Rails framework and all of your application’s classes already loaded. Figure 4.4 illustrates some basic ActiveRecord features by creating some movies in our database, searching for them, changing them, and deleting them (CRUD) . As we describe the role of each set of lines, you should copy and paste them into the console to execute
Image of page 142
the code. The URI accompanying the code example will take you to a copy-and-pastable version of the code on Pastebin. 1 #### Create 2 starwars = Movie.create!(:title => ’Star Wars’, 3 :release_date => ’25/4/1977’, :rating => ’PG’) 4 # note that numerical dates follow European format: dd/mm/yyyy 5 requiem = Movie.create!(:title => ’Requiem for a Dream’, 6 :release_date => ’Oct 27, 2000’, :rating => ’R’) 7 # Creation using separate ’save’ method, used when updating existing records 8 field = => ’Field of Dreams’, 9 :release_date => ’21-Apr-89’, :rating => ’PG’) 10! 11 field.title = ’New Field of Dreams’ 12 #### Read 13 pg_movies = Movie.where("rating = ’PG’") 14 ancient_movies = Movie.where(’release_date < :cutoff and rating = :rating’, 15 :cutoff => ’Jan 1, 2000’, :rating => ’PG’) 16 #### Another way to read 17 Movie.find(3) # exception if key not found; find_by_id returns nil instead 18 #### Update 19 starwars.update_attributes(:description => ’The best space western EVER’, 20 :release_date => ’25/5/1977’) 21 requiem.rating = ’NC-17’ 22! 23 #### Delete 24 requiem.destroy 25 Movie.where(’title = "Requiem for a Dream"’) 26 #### Find returns an enumerable 27 Movie.where(’rating = "PG"’).each do |mov| 28 mov.destroy 29 end Figure 4.4: Although Model behaviors in MVC are usually called from the controller, these simple examples will help familiarize you with ActiveRecord’s basic features before writing the controller. Lines 1–6 (Create) create new movies in the database.
Image of page 143
Image of page 144

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