2013-04-30 51 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(......) 

确保您命名约定。检查它们是否正确。