如何使用rails 2从oracle数据库中获取超过1000条记录
@business_users_loc = User.find(:all,:conditions=>"(users.role_id NOT IN ('#{Role[:submember].id}') AND locations.mac_id IS NOT NULL and users.mac_id IS NOT NULL and locations.location_type='Business')",:order => 'users.created_at DESC',:joins=>:locations,:include=>[{:locations=>[:social_network_mac_id,{:pronto_gateway=>[:heartbeat]},:fb_sponsors_fanpage]}],:select=>"distinct(users.login_slug), users.created_at, users.email, users.login, users.id")
当我尝试上述查询时出现此错误
ActiveRecord::StatementInvalid: OCIError: ORA-01795: maximum number of expressions in a list is 1000: SELECT * FROM "LOCATIONS" WHERE ("LOCATIONS"."ID" IN (19228,18667,14642,15727,13541,14700,....
你能帮我吗....
答案 0 :(得分:0)
您可以查看find_in_batches
方法。
options = {
:conditions=>"users.role_id NOT IN ('#{Role[:submember].id}')",
:batch_size => 1010
}
User.find_in_batches(options) do |group|
# YOUR CODE
end
根据需要更改:batch_size
的值;默认值为1000。
答案 1 :(得分:0)
使用:
User.find_in_batches(:all,:conditions=>"users.role_id NOT IN ('#{Role[:submember].id}')") do |users|
users.each do |user|
#do somthing
end
end