Domanda

I am using twilio to provide audio conference functionality in my rails app. When I call my conference number, twilio passes on a couple of values - including 'From' which contains the caller's phone number in international format.

I have a profile for every user in my system and in my controller I am querying the profile to provide a personalised welcome message. Every profile contains between 0 and 3 numbers (primary, secondary and cellphone) and I need to check the caller's ID against those three fields in all profiles.

When I use the console on my dev machine, the following code finds the correct profile:

Profile.find_by('+44000000000')

When I upload to heroku, I use following code instead:

name = Profile.find_by(params['From']) || 'there'

Which causes an error in my app:

2014-04-03T19:20:22.801284+00:00 app[web.1]: PG::DatatypeMismatch: ERROR:  argument of WHERE must be type boolean, not type bigint
2014-04-03T19:20:22.801284+00:00 app[web.1]: LINE 1: SELECT  "profiles".* FROM "profiles"  WHERE (+4400000000) ...

Any suggestion how that could be solved?

Thanks!


Additional information:

I think my problem is that I don't know how to query either the whole profile or three columns at once. Right now the code:

name = Profile.find_by(params['From'])

is not correct (params['From'] contains a phone number) because I am not telling rails to query the columns primary phone number, secondary phone number and cellphone. Neither am I querying the whole profile which would also be an option.

So the question basically is:

How can I change this code:

Profile.find_by(params['From'])

so that it queries either all fields in all profiles or just the three columns with phone numbers which each profile contains?

Is there something like Profile.where(:primary_number).or.where(:secondary_number)or.where(:cellphone) => params['From']

?

È stato utile?

Soluzione

I am not familiar with twilio and not sure if this helps but find and find_by_attribute_name accepts array of values as options:

name = Profile.find_by([params['From'], 'there'] )

suppose params['From'] was here , This should generate:

 SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`attribute` IN ('here', 'there')

Or:

If you are trying to build dynamic matcher at run time , which is called Meta-programming , you can try this code:

name = eval("Profile.find_by_#{params['From']) || 'there'}(#rest of query params here) ")

Update

First of all, i think you are not using find_by correctly!! the correct syntax is:

Model.find_by(attribute_name: value)
#e.g
Profile.find_by(phone_number: '0123456')

Which will call where and retrive one record, but passing a value will generate a condition that always passes, for example:

Model.find_by('wrong_condition')
#will generate SQL like:
SELECT `models`.* FROM `models` WHERE ('wrong_condition') LIMIT 1
#which will return the first record in the model since there is no valid condition here

Why don't you try:

Profile.where('primary_number = ? OR secondary_number = ? OR cellphone = ?', params['From'], params['From'], params['From'])

Altri suggerimenti

You can write your query like:

Profile.where("primary_number = ? or secondary_number = ? or cellphone = ?", params['From'])

Just double check the syntax, but that should do it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top