2013-04-30 65 views
0

Current_user有許多最喜愛的社區。最喜歡的社區可以通過這個獲取。如何獲取所有關聯的記錄?

@communities = current_user.get_up_voted(Community) 

然後每個社區都有很多這樣的話題。

社區的has_many:Community_topics
Community_topic belongs_to的:社區

現在,我怎麼能獲取所有被屬於CURRENT_USER最喜愛的社區主題?

我想這

@communities = current_user.get_up_voted(Community) 

@community_topics = Community_topics.where(:community_id => @communities).page(params[:page]).order("last_active_at DESC") 

但我得到這個錯誤:(

NameError (uninitialized constant UsersController::Community_topics): 
+1

這是錯字問題?試試這個'CommunityTopic.where(「community_id in(?)」=> @ communities.collect(&:id))' – Amar 2013-04-30 06:00:14

+0

@Amar謝謝。現在我有這個錯誤:('Mysql2 ::錯誤:未知的列community_topics.community_id在(?)在哪裏子句' – MKK 2013-04-30 06:03:29

+0

@Amar請問我請正確的代碼? – MKK 2013-04-30 06:14:45

回答

0

按照documentation信:

A has_many association indicates a one-to-many connection with another model. You’ll often find this association on the 「other side」 of a belongs_to association. This association indicates that each instance of the model has zero or more instances of another model.

確保您的拼寫是否正確,子表中有一個parent_table_id字段,並且您已聲明子表0其父母。

0

如果你犯了一個模型:

e.g. my_model.rb 

它的內容應該looke這樣的:

class MyModel < ActiveRecord::Base 

end 

所以在控制器,你將它稱爲:

@myvariable = MyModel.where(......) 

確保您命名約定。檢查它們是否正確。

相關問題