0

I am getting a NoMethodError when trying to call my "not_coworker_friend" or "coworker_friend" method. I am working in Ruby MVC. This is my current code, As written it does not produce any errors:

class EmergencyTrip < ActiveRecord::Base

belongs_to :user

has_attached_file :receipt
validates_attachment_presence :receipt

validates_presence_of :trip_cost, :if => :not_coworker_friend
validates_presence_of  :phone_of_person, :if => :coworker_friend

def not_coworker_friend
  (self.ride_home_service == "co-worker/friend") ? false : true
end

def coworker_friend
  (self.ride_home_service == "co-worker/friend") ? true : false
end

end

My objective is to only validate the receipt if "not_coworker_friend" == true.

Lines 5 & 6 should be:

...
has_attached_file :receipt, :if => :not_coworker_friend
validates_attachment_presence :receipt, :if => :not_coworker_friend
...

Adding the ":if => not_coworker_friend" to those lines generates a NoMethodError (undefined method `call' for :not_coworker_friend:Symbol):, even though there is no error thrown for lines 8 & 9. Why does this only selectively throw an error? And how can I correct this?

6
  • define method before using it. Just in case. Commented Mar 20, 2017 at 12:59
  • I changed so the method was defined before use but this did not work. There is no error for when it is called on lines 8 & 9 regardless, so it has to be defined. Commented Mar 20, 2017 at 13:03
  • Should work like this. Maybe reload console? but please read up on boolean: en.wikipedia.org/wiki/Boolean_data_type Commented Mar 20, 2017 at 13:09
  • @Fallenhero I have reloaded my console and restarted my whole application many times and rebuilt and clean. Of course it should work like this... but it does not so I am asking why Commented Mar 20, 2017 at 13:13
  • You could try it for now with a Proc.new ... Commented Mar 20, 2017 at 13:16

1 Answer 1

2

try this:

class EmergencyTrip < ActiveRecord::Base

    belongs_to :user

    has_attached_file :receipt, :if => Proc.new{|f| f.ride_home_service != "co-worker/friend"}
    validates_attachment_presence :receipt, :if => Proc.new{|f| f.ride_home_service != "co-worker/friend"}
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! you helped me a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.