Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:49:16+00:00 2026-05-24T09:49:16+00:00

In this exercise from O’Reilly’s Head first Rails (ed. 2009) there are 2 related

  • 0

In this exercise from O’Reilly’s “Head first Rails” (ed. 2009) there are 2 related objects.

The “Flight” object [I used annotate gem to show every attribute]:

# == Schema Information
#
# Table name: flights
#
#  id                :integer         not null, primary key
#  departure         :datetime
#  arrival           :datetime
#  destination       :string(255)
#  baggage_allowance :decimal(, )
#  capacity          :integer
#  created_at        :datetime
#  updated_at        :datetime
#

class Flight < ActiveRecord::Base
  has_many :seats
end

Ant the “Seat” object:

# == Schema Information
#
# Table name: seats
#
#  id         :integer         not null, primary key
#  flight_id  :integer
#  name       :string(255)
#  baggage    :decimal(, )
#  created_at :datetime
#  updated_at :datetime
#

class Seat < ActiveRecord::Base
  belongs_to :flight
end

As you may guess the seat.baggage value should always be less than or equal to seat.flight.baggage_allowance.

So I wrote this validator, which works well:

class Seat < ActiveRecord::Base

  belongs_to :flight

  def validate
      if baggage > flight.baggage_allowance
      errors.add_to_base("Your have to much baggage for this flight!")
    end 
  end  
end

Then I tried to refactor it with this prettier one:

      validates :baggage, :numericality => { :less_than_or_equal_to => flight.baggage_allowance }, :presence => true 

But it causes a NameError in SeatsController:

undefined local variable or method `flight' for #<Class:0x68ac3d8>"

Than I also tried with “self.flight.baggage_allowance”:

validates :baggage, :numericality => { :less_than_or_equal_to => self.flight.baggage_allowance }, :presence => true

But it throws a NoMethodError Exception:

undefined method `flight' for #<Class:0x67e9b40>

Is there a way to make the prettier validator work?

Which is the best practice to do this kind of validation?

Thank you.

—EDIT—

As Maurício Linhares kindly suggested hereafter, the problem is solvable defining a :bagging_allowance symbol.
I’d like to understand better where custom validation is really needed.
What about this one, is it possible to convert even this to a “validates” method or not? and why?

class Seat < ActiveRecord::Base
.
.
.
def validate
  if flight.capacity <= flight.seats.size
    errors.add_to_base("The flight is fully booked, no more seats available!")
  end
end

Thank you again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:49:17+00:00Added an answer on May 24, 2026 at 9:49 am

    You can do it like this:

    class Seat < ActiveRecord::Base
    
      validates :baggage, :numericality => { :less_than_or_equal_to => :baggage_allowance }, :presence => true 
    
      belongs_to :flight
    
      def baggage_allowance
        flight.baggage_allowance
      end  
    end
    

    EDIT

    You can’t do this:

    class Seat < ActiveRecord::Base
            validates :baggage, :numericality => { :less_than_or_equal_to => flight.baggage_allowance }, :presence => true 
    end
    

    Because the method validates is being called at the class level, so there is no flight variable available, as it is an instance variable. When you configure it with :baggage_allowance you tell Rails to call the :baggage_allowance method on an instance of Seat to be able to access the value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a programming exercise from Chapter 6 of The Art and Science of
This is my first effort on solving the exercise. I gotta say, I'm kind
I'm looking few exercise from university about C++ and I found out this exercise:
This is an exercise from the standard ML tutorial seen here: http://homepages.inf.ed.ac.uk/stg/NOTES/node42.html I am
Exercise: There's too much repetition in this file. Use strings, formats, and escapes to
I searched the site but it seems like this exercise from the book Learn
I'm having difficulty writing a program to solve this exercise from a Java text
I am learning python atm, and was doing an exercise from this site --
I need help with this exercise where I have to translate properties from English
This is a simple exercise from Chapter 2 of the Sun Certified Java Associate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.