首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >acts_as_taggable_on_steroids - tags_counts方法出错

acts_as_taggable_on_steroids - tags_counts方法出错
EN

Stack Overflow用户
提问于 2013-03-20 22:25:51
回答 1查看 113关注 0票数 0

tag_count方法为我的GroupsController中的所有记录提供了相同的值:

代码语言:javascript
复制
    @group_blog_tags=@group.blog.blog_posts.tag_counts

哪里

代码语言:javascript
复制
     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

但无论我选择哪一组,它总是获取标记表中的第一条记录。

我做错了什么?

代码语言:javascript
复制
@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">] 

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

EN

回答 1

Stack Overflow用户

发布于 2013-03-20 22:42:09

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

编辑

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

  • A组has_one博客,博客belongs_to a组
  • A组has_many博客,博客belongs_to a组
  • A组belongs_to a博客,博客has_one组
  • A组belongs_to a博客,博客has_many groups

<代码>H19A组has_one_and_belongs_to_many博客,博客has_one_and_belongs_to_many组

x belongs_to y表示您的x表中有一个y_id

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

A x has_and_belongs_to_many y表示x有许多yy has_many x

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15526288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档