0

我有一个设计模型,允许设计师上传他们制作的设计。我想查询由同一设计师上传的同名设计并将它们组合在一起。

我不知道该怎么做。你有什么建议吗?

我运行 Rails 3.2、Ruby 1.9.3 和 Mongoid。

更多信息:

设计师型号:

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

设计型号:

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

设计控制器:

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

我有一个可以上传很多设计的设计师。如果他上传具有相同标题的设计,它们应该组合在一起(我认为最好的方法是在数组中创建一个数组)。我不知道如何在数据库中查询这个。有任何想法吗?

编辑

我想我需要类似的东西:

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

这应该给我一个所有重复标题的实例。当我拥有它时,我可以查询这些标题并将它们放入嵌套数组或类似的东西中。

问题是我不知道您如何在 mongoid 中进行此查询?!

4

1 回答 1

0

您的模型明显不完整,因此请接受以下工作解决方案,以尝试与您的模型保持接近,而答案仅作为继续前进的建议。

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

配置/路由.rb

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

测试/单元/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

测试/功能/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

结果

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
于 2012-05-15T17:19:45.317 回答