0

Not sure exactly how to ask this as I am learning as I go. I am creating/updating user information from my app to a third party CRM system. I have two methods that run successfully with an after_save callback. During testing, I would comment one out so I can test the other but now I need to combine them with an if else statement.

What should happen when combined together is when User is saved, the system will see if a user exists on the CRM system - if agile_id?. If user exists, it will skip down to the update call and send over any updated contact data, but if it doesn't, it will create a new CRM contact record.

The error I am receiving in the browser is:

undefined method `agile_id?' for #<User:0x007ffe24cef318>

user.rb

...
after_save :sync_to_agilecrm
...
def sync_to_agilecrm
    agile_id = AgileCRM.request :get, 'contacts/search/email/'+email, nil
    if agile_id?
      contact_data = {
          'properties': [
              { 'type': 'SYSTEM', 'name': 'first_name', 'value': first_name },
              { 'type': 'SYSTEM', 'name': 'last_name', 'value': last_name },
              { 'type': 'SYSTEM', 'name': 'email', 'subtype': 'work', 'value': email },
              { 'type': 'SYSTEM', 'name': 'address', 'value': '{\"address\":\"225 George Street\",\"city\":\"NSW\",\"state\":\"Sydney\",\"zip\":\"2000\",\"country\":\"Australia\"}' },
          ]
      }
      parsed_contact_data = JSON.parse(contact_data.to_json)
      print(AgileCRM.request :post, 'contacts', parsed_contact_data)
    else
      update_contact_data = {
          'id': agile_id,
          'properties': [
              { 'type': 'SYSTEM', 'name': 'first_name', 'value': first_name },
              { 'type': 'SYSTEM', 'name': 'last_name', 'value': last_name },
              { 'type': 'SYSTEM', 'name': 'email', 'subtype': 'work', 'value': email },
              { 'type': 'SYSTEM', 'name': 'address', 'subtype': 'work', 'value': address_line1 },
          ]
      }
      parsed_update_contact_data = JSON.parse(update_contact_data.to_json)
      print(AgileCRM.request :put, 'contacts/edit-properties', parsed_update_contact_data)
    end
  end
...
Charles Smith
  • 3,201
  • 4
  • 36
  • 80
  • That method is way too long. Separate it into smaller methods which perform a single task. – max Oct 30 '17 at 22:24
  • 1
    I'm guessing you intented to use `if agile_id`. – max Oct 30 '17 at 22:28
  • @max Yes, I am trying to find out if agile_id exists and I wish I could make it shorter, but as mentioned, I am feeling around in the dark just figuring it out :) – Charles Smith Oct 30 '17 at 22:35
  • `?` is used in method names in ruby. Like (!) it has no special significance to the parser. But the convention is that a method that ends with `?` must return a boolean. You cannot use `?` or `!` when naming local variables. – max Oct 30 '17 at 22:54

1 Answers1

2

agile_id and agile_id? aren't the same thing. You'll sometimes see ActiveRecord objects which have record.attribute? which is enabled through some meta programming.

So, when defining a variable such as agile_id, adding a question mark on the end won't just work, nor is it needed. a simple if agile_id should be sufficient.

Daniel Westendorf
  • 3,375
  • 18
  • 23