我有一个国家模型和一个地方模型——一个国家
has_many
一个地方,一个地方
belongs_to
一个国家。一个地方模型也
哈萨尔多
员额(哪些)
belong_to
一个地方)。我想把来自某个国家的所有帖子聚合成一个提要,就像社交网站上的朋友活动提要一样。我很难找到合适的搜索方法,任何帮助都会非常感谢!目前我有:
RB
has_many :places
has_many :posts, :through => :places
def country_feed
Post.from_places_belonging_to(self)
end
邮政银行
belongs_to :place
scope :from_places_belonging_to, lambda { |country| belonging_to(country) }
private
def self.belonging_to(country)
place_ids = %(SELECT place_id FROM places WHERE country_id = :country_id)
where("place_id IN (#{place_ids})", { :country_id => country })
end
end
然后在country show.html.erb文件中:
<h3>Posts</h3>
<% @country.country_feed.each do |post| %>
<%= link_to "#{post.user.username}", profile_path(post.user_id) %> posted on <%=link_to "#{post.place.name}", place_path(post.place_id) %> <%= time_ago_in_words post.created_at %> ago:
<%= simple_format post.content %>
<% end %>
这没有任何错误,但提供了所有职位的信息,而不是为特定国家选择职位。最好的方法是什么?我有一种潜移默化的怀疑,我可能会把事情复杂化……提前谢谢!