Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6637821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:21:36+00:00 2026-05-25T23:21:36+00:00

Hi I have a posts model and a collections model, joined together by a

  • 0

Hi I have a posts model and a collections model, joined together by a collectionship model. When a user makes a post he adds the post to a collection, such as ‘music’. However, when I list all the user’s collections there are multiple ‘music’ entries for each post made instead of just 1.
I’m grabbing the collections with @collections = @user.posts.map(&:collections).flatten, if I add a .uniq on the end get no duplicates (@collections = @user.posts.map(&:collections).flatten.uniq) But could someone explain why I’d have to do this??? Thanks a lot.

UsersController

  def show
    @user = User.find(params[:id]) rescue nil
    @posts = @user.posts.paginate(:per_page => "10",:page => params[:page])
    @title = @user.name
    @collections = @user.posts.map(&:collections).flatten
  end

views/users/show.html.erb

 <h1>Collections</h1>

  <% @collections.each do |collection| %>
    <%= link_to collection.name, user_collection_posts_path(@user, link_name(collection)) %><br />
  <% end %>

collection model

class Collection < ActiveRecord::Base
  mount_uploader :image, CollectionUploader
  attr_accessible :name, :image, :user_id
  has_many :collectionships
  has_many :users, :through => :posts
  has_many :posts, :through => :collectionships
end

collectionship model

class Collectionship < ActiveRecord::Base
  belongs_to :post
  belongs_to :collection
  has_one :user, :through => :post
  attr_accessible :post_id, :collection_id
end

post model

  belongs_to :user
  has_many :collectionships
  has_many :collections, :through => :collectionships

user mdoel

has_many :posts, :dependent => :destroy
has_many :collections, :through => :posts
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:21:37+00:00Added an answer on May 25, 2026 at 11:21 pm

    You’ve got the line that’s causing it. Here’s my take on why you’re seeing what you do (just an expansion of each step of evaluation of that line):

    @user.posts #=>
    [
        <Post1:
            id: 1492
            collections: ['history', 'spain']
        >,
        <Post2:
            id: 1912
            collections: ['history', 'shipwrecks']
        >
    ]
    
    @user.posts.map(&:collections) #=>
    [
        ['history', 'spain'],
        ['history', 'shipwrecks']
    ]
    
    @user.posts.map(&:collections).flatten #=>
    [
        'history',
        'spain',
        'history',
        'shipwrecks'
    ]
    

    So you can see that each for each post, post.collections returns all the collections that post is in (as it should). And the flatten method doesn’t care if there are duplicates or not – it just cares about returning a single, 1-D array. So those duplicates survive throughout the whole operation, unless you call uniq on the final product.

    I believe there’s an ActiveRecord way to avoid this as well: If a User has_many :collections, then @user.collections shouldn’t have any duplicates. Might be an ugly AR macro, though, with so many levels of inheritance.

    Anyway, hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model Post and collection Posts. And want to make form with list
I have a User model that has many posts. Im trying to render the
I have a Topic model and a Post model. A Topic has many Posts,
I have declared models in AppEngine's models.py: class Post(db.Model): topic = db.ReferenceProperty(Topic, collection_name='posts', verbose_name=_('Topic'))
I have 3 model, User, Post, Comment with definition like below class Post <
I have a posts model... I am trying to display this model on the
So for example ... Lets say I have a Posts model and a News
Assuming I have a comments model and a posts model, What code can I
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here
I'm trying to set up my EF Model. I have 2 tables: Posts and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.