2014-06-08 58 views
0

控制器未知列錯誤

@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)): 
+0

的DB不知道選擇哪個'id'列 - 從''用戶的一個或'communities'之一。那麼'current_user.get_up_voted(Community).pluck('users.id')''怎麼樣? – pdobb

+0

@pdobb不是'communities.id'?因爲它試圖獲得社區數組ids – HUSTEN

+0

如果你想獲得社區id,那麼你的查詢應該從社區模型而不是用戶模型開始。 – pdobb

回答

1

只是因爲你叫那列id這是沒有辦法訪問它。當列名在結果表中出現多次時(通常在join的情況下)

例如:像這樣如果我加入users表和user_ranks表。

id | name | email  | id  | rank | user_id 
1 | 'pari' | '[email protected]' | `rank_id`| 12 | 1 

可以通過調用字段名稱明確像

current_user.get_up_voted(Community).pluck('communities.id') 
+0

我試過你的答案。但現在它返回這個錯誤'NoMethodError(未定義的方法'ID爲6:Fixnum): ' – HUSTEN

+0

你能告訴我完整的回溯 –

+0

我喜歡從錯誤發生的地方看到。錯誤回溯看起來像這樣http://www.infoq.com/resource/news/2008/09/rails-exc-getex-hoptoad/en/resources/ht-error-backtrace.png –

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) 
+0

不,我沒有'upvoted_by'我用這個gem代替'acts_as_votable https:// github.com/ryanto/acts_as_votable' – HUSTEN

+0

啊,我明白了。那麼也許不是那麼多。 – pdobb

+0

你能否更新你的答案? – HUSTEN