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 7944885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:41:10+00:00 2026-06-04T00:41:10+00:00

I have a design model, which allows designers to upload a design they have

  • 0

I have a design model, which allows designers to upload a design they have made. I would like to query for designs with the same title, uploaded by the same designer and group them together.

I can’t get my head around on how do I do this in. Do you have a suggestion?

I run Rails 3.2, Ruby 1.9.3 and Mongoid.

More info:

Designers model:

class Designer
    has_many :designs, :order => "created_at"
end

Designs model:

class Design
    belongs_to :designer

    attr_accessible :image,  :tags, :description, :title, :featured

    mount_uploader :image, DesignerUploader

    field :width
    field :height
    field :description
    field :title
    field :tags, type: Array
    field :featured, :type => Boolean, :default => false
end

Designs controller:

def newest
    @designs = Design.select("title, count(title) as title_count").group(:title).having("title_count > 1")
end

I have a designer who can upload a lot of designs. If he uploads designs with the same title, they should be grouped together (I’m thinking the best way for this would be to create an array inside an array). I don’t know how to query for this in the database. Any ideas?

EDIT

I think I need something similar to this:

Design.find(:all, :group => [:title], :having => "count(*) > 1" )

This should give me a single instance of all the duplicates titles. When I have that, I can query for those titles and put them in a nested array or something like that.

Problem is that I don’t know how you can make this query in mongoid?!

  • 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-06-04T00:41:12+00:00Added an answer on June 4, 2026 at 12:41 am

    Your models are significantly incomplete, so please accept the following working solution as an attempt to remain close to your models, and the answer as just a suggestion for moving ahead.

    class Designer
        include Mongoid::Document
        field :name, type: String
        has_many :designs #, :order => "created_at"
    end
    
    class Design
        include Mongoid::Document
        belongs_to :designer
    
        attr_accessible :image, :tags, :description, :title, :featured
    
        #mount_uploader :image, DesignerUploader
    
        field :width, type: Integer
        field :height, type: Integer
        field :description, type: String
        field :title, type: String
        field :tags, type: Array
        field :featured, type: Boolean, default: false
    end
    
    class DesignsController < ApplicationController
      def newest
        @designer = Designer.where(name: params['name']).first
        @designs = Design.where(designer_id: @designer._id).where(title: params['title']).to_a
        render :text => <<-EOF
          params: #{params.inspect}
          designer: #{@designer.inspect}
          designs by title: #{@designs.inspect}:
        EOF
      end
    end
    

    config/routes.rb

    match ':controller/:action/:name/:title'
    

    test/unit/designer_test.rb

    require 'test_helper'
    
    class DesignerTest < ActiveSupport::TestCase
      def setup
        Designer.delete_all
        Design.delete_all
      end
    
      test "designer designs" do
        design_title = 'little black dress'
        designer = Designer.create(name: 'Coco', designs: [
          Design.new(title: design_title, width: 300, height: 400),
          Design.new(title: design_title, width: 600, height: 800),
          Design.new(title: 'jersey traveling suit', width: 600, height: 800),
        ])
        p designer
    
        # finds designs with designer and title on server
        designs = Design.where(designer_id: designer._id).where(title: design_title).to_a
        assert_equal(2, designs.size)
        p designs
    
        # finds all designer's designs and selects locally
        designs = designer.designs.select{|design| design['title'] == design_title}
        assert_equal(2, designs.size)
        p designs
      end
    end
    

    test/functional/designs_controller_test.rb

    require 'test_helper'
    
    class DesignsControllerTest < ActionController::TestCase
      def setup
        Designer.delete_all
        Design.delete_all
      end
    
      test "newest" do
        design_title = 'little black dress'
        designer = Designer.create(name: 'Coco', designs: [
          Design.new(title: design_title, width: 300, height: 400),
          Design.new(title: design_title, width: 600, height: 800),
          Design.new(title: 'jersey traveling suit', width: 600, height: 800),
        ])
        get :newest, :name => 'Coco', :title => design_title
        assert_response :success
        assert_equal('Coco', assigns(:designer).name)
        assert_equal(2, assigns(:designs).size)
        assigns(:designs).each do |design|
          assert_equal(design_title, design.title)
        end
        puts @response.body
      end
    end
    

    result

    Run options: --name=test_newest
    
    # Running tests:
    
          params: {"name"=>"Coco", "title"=>"little black dress", "controller"=>"designs", "action"=>"newest"}
          designer: #<Designer _id: 4fb28df8e4d30bd575000004, _type: nil, name: "Coco">
          designs by title: [#<Design _id: 4fb28df8e4d30bd575000001, _type: nil, designer_id: BSON::ObjectId('4fb28df8e4d30bd575000004'), width: nil, height: nil, description: nil, title: "little black dress", tags: nil, featured: false>, #<Design _id: 4fb28df8e4d30bd575000002, _type: nil, designer_id: BSON::ObjectId('4fb28df8e4d30bd575000004'), width: nil, height: nil, description: nil, title: "little black dress", tags: nil, featured: false>]:
    .
    
    Finished tests in 0.043330s, 23.0787 tests/s, 115.3935 assertions/s.
    
    1 tests, 5 assertions, 0 failures, 0 errors, 0 skips
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this design which I cannot seem to get right, I would like
I have a model for which I would like to save only the attribute
Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
I have a model which contains a link to sql query that fetches information
I would like to have a Referral model, where user A can refer an
I have a Java project that I'm trying to implement with a model-view-controller design.
I have a design of activities like this I have one main activity and
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
My application I have an application design that looks like this: web application layer
I am beginner.Practicing in C# 3.0. The requirement is i have to design model

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.