select DATE(created_at) as DateOnly, count(*)
from comments
where author_id = 877081418
group by DateOnly
order by DateOnly
Thursday, October 29, 2009
You probably don't need an OLAP
Thursday, October 08, 2009
Scope in JavaScript is just from which door you entered
Put simply, we enteredBigComputer
vianew
, sothis
meant “the new object.” On the other hand, we enteredthe_question
viadeep_thought
, so while we’re executing that method,this
means “whateverdeep_thought
refers to”.this
is not read from the scope chain as other variables are, but instead is reset on a context by context basis.
Javascript's scoping has been one of most confusing things about it, just as Ruby's metaclass and object model is the most confusing things about it. If you're looking to expand the horizon of what you understand about programming languages, it's worth it to figure out javascript scoping.
The paragraph gave a good way to think about it: this changes based on the object that calls the method. It only gets confusing when you start passing around functions and using callbacks, which is most of the power of functional programming.
As an example, here, I was using an anonymous function as a callback in the request() method. But it doesn't work!
So that's just one way to solve it. If you're using Prototype, you can also try using the bind() method. jQuery doesn't have an equivalent bind method, as hard as I looked for it at one time. I was just about to write it myself (as it's not too hard), but according to the a list apart article on Getting out of binding situations in javascript:
jQuery does not provide such a binding facility. The library’s philosophy favors closures over binding and forces users to jump through hoops (that is, manually combine lexical closures and
apply
orcall
, much as other libraries do internally) when they actually need to pass along a piece of code referring to “instance members.”
So while I use closures extensively in Ruby, I haven't had to explicitly think about the scope until I was using closures in Javascript. Huzzah. Hopefully, it'll prompt you to take a deeper look at Javascript.
Wednesday, October 07, 2009
Concurrency and integrity with validates_uniqueness_of
Using this validation method in conjunction with ActiveRecord::Base#save does not guarantee the absence of duplicate record insertions, because uniqueness checks on the application level are inherently prone to race conditions.
This could even happen if you use transactions with the ‘serializable’ isolation level. There are several ways to get around this problem:
By locking the database table before validating, and unlocking it after saving. However, table locking is very expensive, and thus not recommended.
By locking a lock file before validating, and unlocking it after saving. This does not work if you‘ve scaled your Rails application across multiple web servers (because they cannot share lock files, or cannot do that efficiently), and thus not recommended.
Creating a unique index on the field, by using ActiveRecord::ConnectionAdapters::SchemaStatements#add_index. In the rare case that a race condition occurs, the database will guarantee the field‘s uniqueness.
Tuesday, October 06, 2009
Amazon S3 and Paperclip plugin
"MethodNotAllowed: The specified method is not allowed against this resource." Error.