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):
4

2 回答 2

0

按照文件的信函:

has_many 关联表示与另一个模型的一对多连接。你会经常在belongs_to 关联的“另一边”找到这个关联。这种关联表明模型的每个实例都有另一个模型的零个或多个实例。

确保您的拼写正确,您parent_table_id在子表中有一个字段,并且您已将子表声明为belongs_to它的父表。

于 2013-04-30T06:08:33.080 回答
0

如果你做一个模型:

e.g. my_model.rb

它的内容应该是这样的:

class MyModel < ActiveRecord::Base

end

所以在控制器中你会调用它:

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

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

于 2013-04-30T06:08:51.470 回答