2013-03-20 63 views
0

的TAG_COUNT方法给出了所有的记录 在我GroupsController我有相同的价值:错误acts_as_taggable_on_steroids - tags_counts方法

@[email protected]_posts.tag_counts 

其中

 class Group < ActiveRecord::Base 
     has_one :blog, :as => :owner, :dependent => :destroy 


     class Blog < ActiveRecord::Base 
      has_many :blog_posts, :order => "blog_posts.created_at desc", :dependent => 
      :destroy 

但它始终是取第一个记录在标签表中,不管我选择哪个组。

我在做什么错?

@group=Group.find(45) => #<Group id: 45, name: "Royal P&O Princess", description:       
     "Royal P&O Princess the next edit", created_at: "2013-03-04 06:04:57", 
     primary_photo_id: 807, updated_at: "2013-03-14 07:33:37", tags: "Caribbean, 
     Nile, Mediterranean", group_memberships_count: 2, group_type: 1, 
     last_updated_by: 1, content_updated_at: "2013-03-04 11:05:08", activity_points: 
     0, activity_status: 0, no_memberships_on: nil, owner_id: 28, sponsor_account_id: 
     65, views: 241, company_id: 0, active: 1, de_flag:0 

@group.blog.blog_posts => [#<BlogPost id: 12, created_at: "2013-03-20 05:08:56", 
     updated_at: "2013-03-20 05:08:56", blog_id: 74, creator_id: 1, title: "South 
     African Sojourn", text: "<p>\r\n\tSouth African Sojourn</p>\r\n", comments_count: 
     0, created_at_year_month: 201303, cached_tag_list: "Africa, Veldt", rating_count: 
     0, rating_total: 0, rating_avg: #<BigDecimal:b2de1978,'0.0',4(8)>, views: 1, 
     source: nil, link: nil, guid: nil, tagline: nil, creator_type: "Profile", 
     num_positive_votes: 0, num_negative_votes: 0, net_helpful: 0, best_image: nil, 
     active: 1>, #<BlogPost id: 11, created_at: "2013-03-20 03:46:07", updated_at: 
     "2013-03-20 03:46:07", blog_id: 74, creator_id: 1, title: "African safari", text: 
     "<p>\r\n\tAfrican safari&nbsp;Africa, Safari&nbsp;Afric...", comments_count: 0, 
     created_at_year_month: 201303, cached_tag_list: "Africa, Safari, Veldt", 
     rating_count: 0, rating_total: 0, rating_avg: #<BigDecimal:b2de1518,'0.0',4(8)>, 
     views: 1, source: nil, link: nil, guid: nil, tagline: nil, creator_type: 
     "Profile", num_positive_votes: 0, num_negative_votes: 0, net_helpful: 0, 
     best_image: nil, active: 1>, #<BlogPost id: 2, created_at: "2013-03-04 06:34:53", 
     updated_at: "2013-03-04 06:34:53", blog_id: 74, creator_id: 28, title: "New blog 
     post", text: "<p>\r\n\tNew blog post</p>\r\n", comments_count: 4, 
     created_at_year_month: 201303, cached_tag_list: "New blog post", rating_count: 0, 
     rating_total: 0, rating_avg: #<BigDecimal:b2de1414,'0.0',4(8)>, views: 7, source: 
     nil, link: nil, guid: nil, tagline: nil, creator_type: "Profile", 
     num_positive_votes: 1, num_negative_votes: 0, net_helpful: 1, best_image: nil, 
     active: 1>] 

     @group.blog.blog_posts.tag_counts   => [#<Tag id: 1, name: "acticity 
    stream message">]  


     Trying another group: 
     Group.find(40).blog.blog_posts.tag_counts => [#<Tag id: 1, name: "acticity 
     stream message">] 

它总是获取与结果相同的记录!

回答

0

我认为一个组“belongs_to”是一个博客,而不是“has_one”。

编辑

您必须指定组和博客之间的关系。它可以是这样的情况:

  • A组HAS_ONE博客,博客belongs_to的一组
  • A组的has_many博客,belongs_to的一组
  • A组belongs_to的博客,博客HAS_ONE组
  • 一个博客
  • A组belongs_to的博客,博客的has_many组
  • A组has_one_and_belongs_to_many博客,博客has_one_and_belongs_to_many组

A x belongs_to y意味着您的x表中有一个y_id

A x has_one yx has_many y指定每个xy的数量。

一个x has_and_belongs_to_many y意味着x有许多yy的has_many x

+0

该组是所有者(即一个组拥有一个博客),我怎么能改变它属于? – user1865578 2013-03-21 04:48:44

+0

@ user1865578我编辑我的帖子以回复。 – pierallard 2013-03-21 08:55:52

+0

我按照你的建议做了更改,即在Blog模型中添加了belongs_to:group,仍然没有运气:(例如:当我做group.blog.blog_posts时,它正确地列出了此组中的所有blog_posts,但是当我尝试获取group.blog.blog_posts.tag_counts,它始终显示相同的结果(对于所有组)...请帮助! – user1865578 2013-03-21 11:13:13