Bird
Raised Fist0
Ruby on Railsframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Why do Rails models represent data in an application?
easy
A. Because models control the server configuration
B. Because models handle user interface design
C. Because models organize and manage data from the database
D. Because models manage network requests

Solution

  1. Step 1: Understand the role of models in Rails

    Models are designed to represent and organize data, connecting to database tables.
  2. Step 2: Identify what models do not handle

    Models do not manage UI, server setup, or network requests; those are handled by other parts of the app.
  3. Final Answer:

    Because models organize and manage data from the database -> Option C
  4. Quick Check:

    Models = Data organization [OK]
Hint: Models = data holders, not UI or server [OK]
Common Mistakes:
  • Confusing models with views or controllers
  • Thinking models handle UI or network
  • Assuming models configure servers
2. Which of the following is the correct way to define a model named Book in Rails?
easy
A. def Book; end
B. class Book < ApplicationRecord; end
C. class Book < Controller; end
D. module Book; end

Solution

  1. Step 1: Recall Rails model inheritance

    Models inherit from ApplicationRecord to connect with the database.
  2. Step 2: Check other options for correctness

    Controllers inherit from Controller, not models; methods and modules are not models.
  3. Final Answer:

    class Book < ApplicationRecord; end -> Option B
  4. Quick Check:

    Model class inherits ApplicationRecord [OK]
Hint: Models inherit ApplicationRecord, not Controller [OK]
Common Mistakes:
  • Using Controller instead of ApplicationRecord
  • Defining model as a method or module
  • Missing inheritance entirely
3. Given the model User with attributes name and email, what will User.first.name return if the first user in the database has name "Alice"?
medium
A. nil
B. "email"
C. An error because name is not defined
D. "Alice"

Solution

  1. Step 1: Understand what User.first does

    This fetches the first user record from the database.
  2. Step 2: Access the name attribute

    Since the first user's name is "Alice", User.first.name returns "Alice".
  3. Final Answer:

    "Alice" -> Option D
  4. Quick Check:

    User.first.name = "Alice" [OK]
Hint: User.first returns first record; access attributes directly [OK]
Common Mistakes:
  • Assuming attribute returns nil if present
  • Thinking name is undefined
  • Confusing attribute with method name
4. What is wrong with this model code? class Product < ApplicationRecord validates :price, presence: true validates :price, numericality: true end
medium
A. The validations are correct and will work as expected
B. The numericality validation is missing an option to allow only numbers
C. The validations should be combined into one line
D. The numericality validation should be numericality: { only_integer: true } if only integers are allowed

Solution

  1. Step 1: Review the model definition

    The class correctly inherits from ApplicationRecord.
  2. Step 2: Check the validations

    Presence ensures not blank, numericality ensures it's a number. Both are correct and standard.
  3. Final Answer:

    The validations are correct and will work as expected -> Option A
  4. Quick Check:

    Model code is syntactically valid and validations proper [OK]
Hint: Presence + numericality is standard for price fields [OK]
Common Mistakes:
  • Thinking numericality requires additional options like only_integer
  • Believing validations must be combined into one
  • Assuming only_integer is required without specification
5. You want to create a model Order that stores customer orders with a status attribute limited to 'pending', 'shipped', or 'cancelled'. How should you represent this in the model to enforce these allowed values?
hard
A. Use a validation like validates :status, inclusion: { in: ['pending', 'shipped', 'cancelled'] }
B. Use a database enum type without any model validation
C. Use a string attribute without validation and check status manually in controllers
D. Use validates :status, presence: true only

Solution

  1. Step 1: Understand the need to limit allowed values

    The status attribute should only accept specific strings.
  2. Step 2: Use model validation to enforce allowed values

    Using validates :status, inclusion: { in: [...] } ensures only allowed statuses are saved.
  3. Final Answer:

    Use a validation like validates :status, inclusion: { in: ['pending', 'shipped', 'cancelled'] } -> Option A
  4. Quick Check:

    Status inclusion validation enforces allowed values [OK]
Hint: Use inclusion validation to limit attribute values [OK]
Common Mistakes:
  • Relying only on presence validation
  • Skipping model validation and checking in controllers
  • Assuming database enum alone is enough without model checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard