sprintf("%0.2f","123.9")
Notes to self
Thursday, May 8, 2008
Tuesday, May 6, 2008
File.new
File.new(file_name).close won't do what I intend to do, i.e. create a file. Instead File.new(file_name,'w').close does the trick. The problem is in the first case the second argument defaults to 'r' which means read. Its strange that parameter is defaulted to read only. When we create file we normally mean we are creating it to write something to the file. So it must default to 'w'.
Saturday, May 3, 2008
DangerousAttributeError
I came across this weird sounding exception while creating a record. At first I didn't know what was wrong with my table. After a little probing I figured it was being caused by an attribute called 'frozen' in my table. I was using it to indicate if that record is frozen or not. But it appears Rails uses this keyword internally for the same purpose I was using that attribute for. I had to change this attribute to a different name (I named it 'frosen') for it to work.
This is what Rails api says about the error:
Raised when attribute has a name reserved by ActiveRecord (when attribute has name of one of ActiveRecord instance methods).
http://api.rubyonrails.com/classes/ActiveRecord/DangerousAttributeError.html
This is what Rails api says about the error:
Raised when attribute has a name reserved by ActiveRecord (when attribute has name of one of ActiveRecord instance methods).
http://api.rubyonrails.com/classes/ActiveRecord/DangerousAttributeError.html
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:
That was producing the SQL statement:
But that statement didn't yield any result set.
Whereas the following code:
..produces the SQL statement:
which gave me the correct result set.
The difference between the 2 SQL statements is the usage of single-quotes around the numbers.
: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.
Monday, March 24, 2008
Generating unique random numbers
Interesting techniques:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/fd64775134bd67df/dfff415085115cf0
Best one:
Another one:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/fd64775134bd67df/dfff415085115cf0
Best one:
irb(main):001:0> (1..13).sort_by{rand}[0..7]
=> [7, 3, 8, 2, 11, 4, 1, 9]Another one:
ar = []
while ar.length < 8
ar << rand(12) + 1
ar.uniq!
end
Subscribe to:
Comments (Atom)