0
0
Ruby on Railsframework~10 mins

Why models represent data in Ruby on Rails - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a model class in Rails.

Ruby on Rails
class User < ApplicationRecord
  # This model represents the users table in the database
  [1]
end
Drag options to blanks, or click blank then click option'
Avalidates :name, presence: true
Bdef hello; puts 'Hi'; end
Cbelongs_to :post
Drender :show
Attempts:
3 left
💡 Hint
Common Mistakes
Adding controller or view code inside the model.
Using methods unrelated to data validation.
2fill in blank
medium

Complete the code to associate a model with another model.

Ruby on Rails
class Post < ApplicationRecord
  [1] :user
end
Drag options to blanks, or click blank then click option'
Abelongs_to
Bhas_many
Cvalidates
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using has_many instead of belongs_to here.
Adding validations instead of associations.
3fill in blank
hard

Fix the error in the model method that returns a user's full name.

Ruby on Rails
class User < ApplicationRecord
  def full_name
    [1]
  end
end
Drag options to blanks, or click blank then click option'
Afirst_name.concat(last_name)
Bfirst + ' ' + last
Cself.first_name + last_name
Dfirst_name + ' ' + last_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong attribute names.
Concatenating without space.
4fill in blank
hard

Fill both blanks to create a scope that finds active users ordered by creation date.

Ruby on Rails
class User < ApplicationRecord
  scope :active, -> { where([1]: true).order([2]: :desc) }
end
Drag options to blanks, or click blank then click option'
Aactive
Bcreated_at
Cupdated_at
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using status instead of active for filtering.
Ordering by updated_at instead of created_at.
5fill in blank
hard

Fill all three blanks to define a model with validations and a method that returns a greeting.

Ruby on Rails
class Customer < ApplicationRecord
  validates :email, presence: true, uniqueness: true

  def greet
    "Hello, [1] [2]!"
  end

  scope :[3], -> { where(active: true) }
end
Drag options to blanks, or click blank then click option'
Afirst_name
Blast_name
Cactive_customers
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using email in the greeting instead of names.
Naming the scope something unrelated.