开发者

IN clause using active record query base in order to create list of integers

I am trying to replicate some SQL functionality using ActiveRecord. My problem relates to my use of a the ruby join method (not be confused with a SQL join). Here is my code:

scope :stats_tips_given, lambda { |date|
  where("created_at >= ? AND tipper_id IN(?)",date, User.stats_users(date).collect(&:id).join(', '))
  }

However the resultant sql query has this result: SELECT "tip_events".* FROM "tip_events" WHERE (created_at >= '2011-04-14' AND tipper_id IN('4, 5, 11, 17, 22, 48, 54, 65, 88, 103, 147, 151, 181, 182, 189, 195, 190, 196, 202, 226, 227, 231, 243, 245, 232, 225, 212, 217, 220, 263, 265, 273, 281, 282, 284, 286, 293, 271, 299, 300, 309, 310, 312, 318, 321, 308, 303, 297, 333, 346, 362, 368, 377, 386, 389, 392, 353, 398, 427, 420, 434, 418, 454, 456, 477, 484, 480, 453, 450, 452, 458, 497, 498, 503, 510, 511, 515, 522, 529, 537, 540, 508, 499, 524, 521, 502, 542, 546, 548, 557, 559, 571, 575, 576, 581, 587, 562, 580, 544, 567, 565, 573, 577, 597, 606, 619, 620, 640, 636, 607, 603, 600, 596, 656, 657, 668, 676, 683, 685, 662, 677, 669, 689, 678, 690, 694, 514, 206, 304, 601, 63, 495, 150, 344, 691, 490, 545, 634, 222, 288, 534, 630, 569, 开发者_StackOverflow中文版323, 697, 489, 394, 568, 661, 672, 130, 381, 590, 205, 527, 474, 184, 622'))

This query would be fine if it didn't have the single quote around the list of numbers. How do I rectify this?


You're forming a string by using join. Just pass it the actual array instead:

User.stats_users(date).collect(&:id)

Instead of:

User.stats_users(date).collect(&:id).join(', ')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜