Question

I have the following model

class TagType
  include DataMapper::Resource

  ## Relationships
  belongs_to :category
  has n, :tags

  ## Properties
  property :uuid, UUID, :key => true, :default => lambda { |r,p| SecureRandom.uuid }
  property :name, Enum[:venue, :format, :genre, :organization]
end

In my app controller, I receive a name parameter as a string, turn it into a symbol, and try to perform the lookup:

get ':cat_name/:tag_type' do
cat = Category.first :name => params[:cat_name]
halt 400, "Invalid category" if cat.nil?
sym = params[:tag_type].to_sym
puts "Sym: #{ sym.inspect }"
raise "Not symbol!" if sym.class != Symbol
tag_type = TagType.first(:category => cat, :name => sym)
halt 400, "Invalid tag type name" if tag_type.nil?

This is giving me

4) Error: test_0001_should_get_all_the_tags_for_a_category(TagController): TypeError: can't convert String into Integer test/app/controllers/tag_controller_test.rb:10:in []' test/app/controllers/tag_controller_test.rb:10:inblock (2 levels) in '

The output for puts "Sym: #{ sym.inspect }" is Sym: :venue I have tried just putting a literal :genre in place of sym to ensure that works ok, and it does. I try to raise an exception if it's not a symbol, but this doesn't fire and every time, it ends up throwing this error despite it clearly being a symbol.

This is using the DataMapper extension dm-types, and more specifically the Enum class

Était-ce utile?

La solution

If you look at my exception, you'll see I missed a vital detail:

) Error: test_0001_should_get_all_the_tags_for_a_category(TagController): TypeError: can't convert String into Integer test/app/controllers/tag_controller_test.rb:10:in []' test/app/controllers/tag_controller_test.rb:10:inblock (2 levels) in

This was on line 10 of my test which happened to match up with the line in the app controller itself! DataMapper is working fine. My fault.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top