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