0

tag_countメソッドは、GroupsControllerのすべてのレコードに同じ値を与えます。

    @group_blog_tags=@group.blog.blog_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">] 

結果として常に同じレコードをフェッチします!

4

1 に答える 1

0

グループは「has_one」ではなく、ブログ「belongs_to」だと思います。

編集

グループとブログの関係を指定する必要があります。次のような場合があります。

  • グループhas_1つのブログ、ブログbelongs_to a group
  • グループhas_manyblogs、ブログbelongs_to a group
  • グループはブログに属し、ブログはhas_oneグループに属します
  • グループはブログに属し、ブログにはhas_manyグループがあります
  • グループhas_one_and_belongs_to_manyブログ、ブログhas_one_and_belongs_to_manyグループ

Aは、テーブルx belongs_to yにaがあることを意味します。y_idx

Ax has_one yまたは各x has_many yの数を指定します。yx

x has_and_belongs_to_many y含意xにはmanyyとhas_manyがありyますx

于 2013-03-20T14:42:09.247 に答える