-
Using the new/save syntax, create a student with a first and last name and an age user = User.new(first_name: "Rachel", last_name: "Routson", age: 31)
-
Save the student to the database user.save
-
Using the find/set/save syntax update the student's first name to Myles user.first_name = "Miles" user.save
-
Delete the student (where first_name is Myles)
user.destroy
- In your model, validate that every Student's last name is unique
class User < ActiveRecord::Base
validates_presence_of :last_name
end
- In your model, validate that every Student has a first and last name that is longer than 4 characters
class User < ActiveRecord::Base
validates :first_name, :last_name
presence: true,
length {minimum: 4}
end
-
Validate that every first and last name cannot be empty in the code from question 6- presence: true
-
Create a migration that adds a column with a type of string called favorite_color to the students table (don't forget to run rake db:migrate after and for this question write the command in terminal to generate this migration)
class AddColorToUsers < ActiveRecord::Migration
def change
add_column :users, :favorite_color, :string
end
end
-
Using the create syntax create a student named John Doe who is 23 years old and has a favorite_color of purple student = User.create(first_name: "John", last_name: "Doe", age: 23, favorite_color: "purple")
-
Show if this new student entry is valid User.where(first_name: "John")
-
Show the number of errors for this student instance
, @messages={:last_name=>["is too short (minimum is 4 characters)"]}>
-
Using update, Change John Doe's name to Martin Fowler student.first_name = "Martin" student.save student.last_name = "Fowler" student.save
-
Save Martin Fowler student.save
-
Find all of the Students User.all
-
Find the student with an ID of 128 and if it does not exist, make sure it returns nil and not an error Student.find_by_id(128)
-
Find the first student in the table Student.first
-
Find the last student in the table Student.last
-
Find the student with the last name of Fowler Student.where(last_name: "Fowler")
-
Find all of students who have the first name of Martin and a favorite color of red Student.where(favorite_color: "red", first_name: "Martin")
-
Find all of the students and limit the search to 5 students, starting with the 2nd student and finally, order the students in alphabetical order Student.offset(2).limit(5).order(last_name: asc)
-
Delete Martin Fowler (but first look up who he is!) Student.where(first_name: Martin") martin = _ martin.destroy
-
Delete all of the students Student.destroy_all
Bonus
- Use the validates_format_of and regex to only validate names that consist of letters (no numbers or symbols) and start with a capital letter
- Write a custom validation to ensure that no one named Elie Schoppik, Colt Steele or Tim Garcia is included in the students table.