我试图从mysql数据库中获取用户,如果他们已经创建了一个注释,weather type = 'cloud','rain','clear'而不是那些错过了这三个中的一个.
例如.
id user_id weather_type
1 12 cloud
2 12 rain
3 12 clear
4 14 rain
5 15 clear
Run Code Online (Sandbox Code Playgroud)
现在这里只有用户12创建了所有三个音符,weather_type所以只有12个应该被提取而不是14和15.
我必须插入大数据,比如说 20k,我怀疑我是否编写了优化查询。
它能做什么:
这是代码。
merge_users = MergeField.get_user_field_values(notification_template.merge_field, scope_users) #=> **returns users 1k - 20k**
if merge_users.present?
merge_users.each_slice(100) do |record|
record.each do |user_record|
user = User.find_by(id: user_record.user_id)
text = notification_template.title
notification_template.description = MustacheDescription.render(notification_template, user_record)
text += " " + notification_template.description
Rails.logger.info "Merge field message: #{notification_template.description}"
construct_user_notifications(notification_template, user_record.user_id) #=> **this calls another method below which actually …Run Code Online (Sandbox Code Playgroud) ruby activerecord ruby-on-rails ruby-on-rails-3 ruby-on-rails-4
select user_id, max(perception_score) as max, min(perception_score) as min from temp_user_notes group by user_id as t1;
Run Code Online (Sandbox Code Playgroud)
我正在尝试在 Rails Active Record 中转换此 sql 查询,但很难创建别名
我在JSON中返回用户字段的响应.我正在创建JSON,如下所示.
def user_response(users)
users_array = []
users.each do |user|
uhash = {}
uhash[:id] = user.id,
uhash[:nickname] = user.nickname,
uhash[:online_sharing] = user.online_sharing,
uhash[:offline_export] = user.offline_export,
uhash[:created_at] = user.created_at,
uhash[:app_opens_count] = user.app_opens_count,
uhash[:last_activity] = user.last_activity,
uhash[:activity_goal] = user.activity_goal,
uhash[:last_activity] = user.last_activity,
uhash[:region] = user.region
users_array << uhash
end
users_array
end
Run Code Online (Sandbox Code Playgroud)
但反应非常奇怪.:id哈希中的键有一个所有字段的数组,不知道为什么.
{
"nickname": "adidas",
"online_sharing": null,
"offline_export": null,
"created_at": "2016-08-26T09:03:54.000Z",
"app_opens_count": 29,
"last_activity": "2016-08-26T09:13:01.000Z",
"activity_goal": 3,
"region": "US",
"id": [
9635,
"adidas",
null,
null,
"2016-08-26T09:03:54.000Z",
29,
"2016-08-26T09:13:01.000Z",
3,
"2016-08-26T09:13:01.000Z",
"US" …Run Code Online (Sandbox Code Playgroud)