Prev | Current Page 264 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

each do |operator|
define_method(operator) do |operand|
@operator = operator
@operand = operand
end
end
end
end
After executing our block, we have an array full of Column objects for which we know
exactly what column was specified and how and what they were compared to. We??™re well over
halfway there now. All we need to do is translate the resulting Column objects into a format like
find??™s :conditions array. We have the condition operators stored in our objects, but the problem
is that SQL doesn??™t use all the same operators as Ruby. We can use the following mapping
to make sure we??™re using the right SQL syntax:
module RQuery
class Column
OPERATOR_MAP = {
:== => "=",
:>= => ">=",
:<= => "<=",
:> => ">",
:< => "<",
:=~ => "LIKE"
}
def to_find_conditions
[ "#{name} #{OPERATOR_MAP[operator]} ?", operand ]
end
end
end
There, when we call to_find_conditions, we??™ll get the right SQL syntax. Next, we need to
convert and merge the whole array of Column objects into one big array we can pass to find_
without_rquery.


Pages:
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276