Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How can I fix ActiveRecord::RecordInvalid: Validation failed:...


  • Please log in to reply
3 replies to this topic

#1 Vell

Vell

    Dispatcher

  • Members
  • 42 posts
  • LocationWashington, DC

Posted 21 August 2013 - 04:54 AM

Hello all,

 

I am currently writing model tests in rails 4. I am attempting to add an error to a reservation object if the total number of reservations for a given date and time has reached a pre-determined limit.

 

When my test runs, it is hitting the appropriate code but it is raising the following error as opposed to just giving me an error in the object that I can use. 

ActiveRecord::RecordInvalid: Validation failed: Reservation limit reached for this time slot
    test/models/reservation_test.rb:29:in `block (2 levels) in <class:ReservationTest>'

I was wondering if anyone can give me an ideas of how to deal with this.

 

My test environment is using shoulda, faker, and factory_girl

 

test/models/reservation_test.rb:

    setup do
      List.create(name: 'reservation_limit', values: 20)
      @reservation = FactoryGirl.build(:reservation)
    end

should "remove unavailable times if reservation limit for time slot is full" do
      20.times{ FactoryGirl.create :reservation }
      assert_equal false, FactoryGirl.create(:reservation), "reservation was created after total reservation for time slot was reached."
    end

app/models/reservation.rb

def check_reservation_time
    errors.add(:base, "Can't set reservation to a time before 5pm.") if parse_time(start_time) < parse_time("17:00")
      
    errors.add(:base, "Can't set reservation to a time after 8pm.") if parse_time(start_time) > parse_time("20:00")
      
    errors.add(:base, "Reservation limit reached for this time slot") if reservation_limit_reached?(Reservation.where("start_date = ? and start_time = ?", self.start_date, self.start_time))
  end

  def reservation_limit_reached?(reservations)
    reservations.size >= List.where(name: 'reservation_limit').first.values.to_i
  end

Any ideas or help is appreciated. Thanks.

 



#2 Kelli Shaver

Kelli Shaver

    Inspector

  • Administrators
  • 75 posts
  • LocationKentucky

Posted 21 August 2013 - 07:07 AM

I think you could do something like.....

 

rescue_from ActiveRecord::RecordInvalid, :with => :limit_reached

def limit_reached
end


#3 Vell

Vell

    Dispatcher

  • Members
  • 42 posts
  • LocationWashington, DC

Posted 21 August 2013 - 09:39 PM

Thanks for the response Kelli.  I just tried adding this to my Reservation model and I get a no method error:

NoMethodError: undefined method `rescue_from' for #<Class:0x007fd5d1b609c8>
    app/models/reservation.rb:2:in `<class:Reservation>'
    app/models/reservation.rb:1:in `<top (required)>'
    test/models/reservation_test.rb:6:in `block (2 levels) in <class:ReservationTest>'

I noticed when i started researching rescue_from, most people have this placed in the Applications controller. I see that the top of the controller looks like 

class ApplicationController < ActionController::Base

Is it that rescue_from only works in the controller? If so, I am not able to understand how running a model test would invoke something in the controller (unless I'm missing something :-)). Also, how does an error like this rear its ugly head? I can see when I comment out that assertions of the specific test, I don't run into this ActiveRecord::RecordInvalid error.



#4 Vell

Vell

    Dispatcher

  • Members
  • 42 posts
  • LocationWashington, DC

Posted 22 August 2013 - 05:18 PM   Best Answer

Interesting. I solved the issue finding a post from a person with the same error while trying to test. Issue was that i was trying to create an invalid object before I could test it. So instead, I assigned a valid object using Factory girl to a local variable then tested that the save was false and this resolved my issue.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users