Monday, April 21, 2008

POSTing and PUTting from cURL

>curl -X PUT -d "post[title]=first post from curl" "http://localhost:3000/posts/1" -H "Accept: text/xml"

>curl -d "post[title]=first post from curl" "http://localhost:3000/posts" -H "Accept: text/xml"

-X POST is not needed in the second command because the default is POST when -d flag is used.

Thursday, April 17, 2008

Useful information in the request

Sometimes I have to extract some information from the incoming HTTP request. Most often I might need it for usage reports like browser or referring websites, etc. Here are some common bits I use:
Browser and OS => request.env['HTTP_USER_AGENT']
Referring website => request.env["HTTP_REFERER"]
IP => request.remote_ip
Session id => session.session_id

Wednesday, April 9, 2008

Behaviour of 'in' clause

I had this following code in one of my finder statements:
:conditions => ["subscription_id IN (?)",subscription_ids.join(",")]

That was producing the SQL statement:
SELECT * FROM memberships WHERE (subscription_id IN ('640,610,641,639,642,620,645'))

But that statement didn't yield any result set.

Whereas the following code:
:conditions => "subscription_id IN (#{subscription_ids.join(',')})"

..produces the SQL statement:
SELECT * FROM memberships WHERE (subscription_id IN (640,610,641,639,642,620,645))

which gave me the correct result set.

The difference between the 2 SQL statements is the usage of single-quotes around the numbers.