This preview shows page 142 - 144 out of 517 pages.
Self-Check 4.2.1.In line 3 of Figure4.3, how many arguments are we passing tocreate_table, and ofwhat types?Two arguments: the first is a string and the second is a block. We used poetry mode, allowing us toomit parentheses.Self-Check 4.2.2.In Figure4.3, the ____ method yields ____ to the block.create_table; the variablet4.3 Models: Active Record BasicsWith ourMoviestable ready to go, we’ve completed the first three steps—app creation, routing, andinitial migration—so it’s time to write some app code. The database stores the model objects, but as wesaid in Chapter2, Rails uses the Active Record design pattern to “connect” models to the database, sothat’s what we will explore next. Create a fileapp/models/movie.rbcontaining just these three lines:1classMovie<ActiveRecord::Base2attr_accessible:title,:rating,:description,:release_date3endThanks to convention over configuration, those three lines inmovie.rbenable a great deal of behavior.To explore some of it, stop the running application with Control-C and instead runrailsconsole,which gives you an interactive Ruby prompt likeirb(main):001.0>with the Rails framework and allof your application’s classes already loaded. Figure4.4illustrates some basic ActiveRecord features bycreating 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
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.
Temple University Fox School of Business ‘17, Course Hero Intern
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.
University of Pennsylvania ‘17, Course Hero Intern
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.
Tulane University ‘16, Course Hero Intern
Stuck? We have tutors online 24/7 who can help you get unstuck.
Ask Expert Tutors
You can ask
You can ask
You can ask
(will expire )