From the course: Ruby on Rails 5 Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Model attributes - Ruby on Rails Tutorial
From the course: Ruby on Rails 5 Essential Training
Model attributes
- [Instructor] In this movie, we will learn how model attributes work. If we were working with a pure Ruby class, we would need to define any attributes on the object using code something like this. I'm actually defining two attribute accessors here. I'm defining them two different ways. The first is the shorthand. I'm calling attr_accessor, and then :first_name to tell it that the name of the attribute should be first_name. That will allow me to read and write to the variable first_name on this class. Or I can write it out the longer way, which really shows what's going on. I can have a reader, which is last_name and it just returns the value of the instance variable last_name. And I can have a writer method, which sets a value equal to last_name, so last_name equals some value. We'll set the instance variable last_name equal to a value. These do the same thing. So they're equivalent, first_name and last_name would behave the same. But I had to write them out, and that's very…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.