Jump to content

The ultimate community for Ruby on Rails developers.


Photo

loop in a model method doesn't have access to other model methods?


  • Please log in to reply
2 replies to this topic

#1 Vell

Vell

    Dispatcher

  • Members
  • 42 posts
  • LocationWashington, DC

Posted 22 August 2013 - 05:48 PM

So as I resolve one issue another issue pops up :) Interestingly enough this one didn't seem to come about till I resoved the other issue but I can't seem to figure out how they are related and I have tried a few ways to solve it with no luck.

 

So in my model I have the following method:

def set_reservation_times_based_on_reservation_limit(date)
    reservation_times = List.where(name: 'reservation_start_times').first.values.split("|")
    
    reservations_for_date_grouped_by_start_time(date).each { |time, reservations| reservation_times.delete(time) if reservation_limit_reached?(reservations) }
    
    return reservation_times
  end

This method is  to remove unavailable times once a predetermined limit has been reached for reservations. Yes it is the same one that my last post was about :)

 

But in this case now, the issue is that I have a check for the number of reservations reservation_limit_reached?(reservations)

 

To which I am getting a no method error. But the method exists:

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

This is the error:

NoMethodError: undefined method `reservation_limit_reached?' for #<Class:0x007fadd03fa4a0>
    app/models/reservation.rb:44:in `block in set_reservation_times_based_on_reservation_limit'
    app/models/reservation.rb:44:in `each'
    app/models/reservation.rb:44:in `set_reservation_times_based_on_reservation_limit'
    test/models/reservation_test.rb:32:in `block (2 levels) in <class:ReservationTest>'
  • I have tried to add "::" in front of the `reservation_limit_reached?(reservations)` but that just brings up different error.
  • I also tried adding 'self' in front of the method and tried calling it as `Reservation.reservation_limit_reached?(reservations)` but still no luck

Is there anything I missed? or that I have not tried?



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 22 August 2013 - 05:57 PM

You probably meant for the reservation_limit_reached? method to be a class method, as it doesn't do anything to one particular reservation, but rather to the collection.

def set_reservation_times_based_on_reservation_limit(date)
  reservation_times = List.where(name: 'reservation_start_times').first.values.split("|")
    
  reservations_for_date_grouped_by_start_time(date).each { |time, reservations| reservation_times.delete(time) if Reservation.reservation_limit_reached?(reservations) }
    
  reservation_times
end

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

  • james likes this

Blog: http://ohm.sh | Twitter: madsohm


#3 Vell

Vell

    Dispatcher

  • Members
  • 42 posts
  • LocationWashington, DC

Posted 22 August 2013 - 06:06 PM   Best Answer

Good grief. I didn't read the error more closely.

 

Yes you are right. That is the best way to call it. So setting the method as a class method and calling it as such does work. The issue then was not with that piece of code but another piece of code where I am calling that same method again. I was so consumed with this piece of code that I didn't think about the rest of the model. Once I corrected the other piece of code all errors went away.

 

Thanks for the reply. I really appreciate it.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users