使用pluck时出现未知的列错误

时间:2014-06-08 21:59:27

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

控制器

@communities = current_user.get_up_voted(Community).pluck(:id) 
@codes = Code.includes(:user).where(:community_id => @communities).order('users.last_active_at DESC').page(params[:page]).per(10)

我收到此错误

ActiveRecord::StatementInvalid (Mysql2::Error: Column 'id' in field list is ambiguous: SELECT id FROM communities` INNER JOIN votes ON votes.votable_id = communities.id AND votes.votable_type = 'Community' WHERE votes.voter_id = 2 AND votes.voter_type = 'User' AND votes.vote_flag = 1 AND (communities.deleted_at IS NULL)):

UPDATE1(带有爱国者代码的SQL查询)

SELECT  `codes`.`id` AS t0_r0, `codes`.`user_id` AS t0_r1, `codes`.`community_id` AS t0_r2, `codes`.`invisible` AS t0_r3, `codes`.`code` AS t0_r4, `codes`.`greeting` AS t0_r5, `codes`.`created_at` AS t0_r6, `codes`.`updated_at` AS t0_r7, `codes`.`deleted_at` AS t0_r8, `codes`.`notification` AS t0_r9, `codes`.`wanted` AS t0_r10, `users`.`id` AS t1_r0, `users`.`email` AS t1_r1, `users`.`encrypted_password` AS t1_r2, `users`.`username` AS t1_r3, `users`.`reset_password_token` AS t1_r4, `users`.`reset_password_sent_at` AS t1_r5, `users`.`remember_created_at` AS t1_r6, `users`.`sign_in_count` AS t1_r7, `users`.`current_sign_in_at` AS t1_r8, `users`.`last_sign_in_at` AS t1_r9, `users`.`current_sign_in_ip` AS t1_r10, `users`.`last_sign_in_ip` AS t1_r11, `users`.`banned` AS t1_r12, `users`.`confirmation_token` AS t1_r13, `users`.`confirmed_at` AS t1_r14, `users`.`confirmation_sent_at` AS t1_r15, `users`.`unconfirmed_email` AS t1_r16, `users`.`created_at` AS t1_r17, `users`.`updated_at` AS t1_r18, `users`.`deleted_at` AS t1_r19, `users`.`last_active_at` AS t1_r20, `users`.`comments_count` AS t1_r21, `users`.`follows_count` AS t1_r22, `users`.`codes_count` AS t1_r23, `users`.`communities_count` AS t1_r24, `users`.`nomail` AS t1_r25, `users`.`point_added_at` AS t1_r26, `user_profiles`.`id` AS t2_r0, `user_profiles`.`user_id` AS t2_r1, `user_profiles`.`language_id` AS t2_r2, `user_profiles`.`country_id` AS t2_r3, `user_profiles`.`prefecture_id` AS t2_r4, `user_profiles`.`gender_id` AS t2_r5, `user_profiles`.`nickname` AS t2_r6, `user_profiles`.`introduction` AS t2_r7, `user_profiles`.`picture_url` AS t2_r8, `user_profiles`.`created_at` AS t2_r9, `user_profiles`.`updated_at` AS t2_r10, `user_profiles`.`deleted_at` AS t2_r11, `user_profiles`.`user_avatar_file_name` AS t2_r12, `user_profiles`.`user_avatar_content_type` AS t2_r13, `user_profiles`.`user_avatar_file_size` AS t2_r14, `user_profiles`.`user_avatar_updated_at` AS t2_r15, `user_profiles`.`age` AS t2_r16, `user_profiles`.`activity_invisible` AS t2_r17, `user_profiles`.`total_point` AS t2_r18, `user_profiles`.`bonus_point` AS t2_r19, `user_profiles`.`introduction_html` AS t2_r20, `user_profiles`.`title` AS t2_r21, `user_profiles`.`next_level` AS t2_r22, `user_profiles`.`invitation` AS t2_r23, `user_profiles`.`notification` AS t2_r24, `user_profiles`.`notification_time` AS t2_r25, `user_profiles`.`wall_time` AS t2_r26, `user_profiles`.`wall_flag` AS t2_r27, `user_profiles`.`wanted_at` AS t2_r28, `user_profiles`.`wanted_message` AS t2_r29 FROM `codes` LEFT OUTER JOIN `users` ON `users`.`id` = `codes`.`user_id` LEFT OUTER JOIN `user_profiles` ON `user_profiles`.`user_id` = `users`.`id` WHERE `codes`.`community_id` IN (6, 2, 9, 1, 8, 16, 18, 19, 20, 21, 22, 23, 24, 30, 29, 67, 66, 5, 87) AND (`codes`.`deleted_at` IS NULL) ORDER BY users.last_active_at DESC LIMIT 10 OFFSET 0  

UPDATE2

@communities = current_user.get_up_voted(Community)
@codes = Code.includes(:user).where(:community_id => @communities.collect(&:id)).order('created_at DESC').page(params[:page]).per(10)

SQL查询

ActiveRecord::StatementInvalid (Mysql2::Error: Column 'id' in field list is ambiguous: SELECT id FROM `communities` INNER JOIN `votes` ON `votes`.`votable_id` = `communities`.`id` AND `votes`.`votable_type` = 'Community' WHERE `votes`.`voter_id` = 2 AND `votes`.`voter_type` = 'User' AND `votes`.`vote_flag` = 1 AND (`communities`.`deleted_at` IS NULL)):

2 个答案:

答案 0 :(得分:1)

只是因为你调用了那个无法访问它的列id。当列名在结果表中多次出现时(通常在join

的情况下会发生这种情况

例如:如果我加入users表和user_ranks表,就像这样。

id | name   | email     | id       | rank | user_id
1  | 'pari' | 'x@g.com' | `rank_id`| 12   | 1

您可以通过明确调用

之类的字段名称来解决此问题
current_user.get_up_voted(Community).pluck('communities.id') 

答案 1 :(得分:1)

您是否在upvoted_by等社区拥有范围?例如。 Community.upvoted_by(user)?如果是这样(或者你添加一个),那么你可以这样做:

Community.upvoted_by(user).pluck(:id)

获取这些社区ID的列表。但是,我认为最好的答案是让ActiveRecord和ARel为你承担一些负担。你可以.merge这样的范围:

# community.rb
scope :upvoted_by, -> user { where(user_id: user) }

# controller
Code.includes(user: { :communities }).merge(Community.upvoted_by(user)).order('users.last_active_at DESC').page(params[:page]).per(10)