1

我正在为 Web 服务编写客户端,他们的模型的一部分是属于用户的字符串列表,其中包含用户以前使用的用户名。我正在尝试将 DataMapper 用于我的客户端 gem,我的第一个想法是使用 DataMapper 的 has n 语法,但我似乎无法将其应用于字符串。有没有更好的方法来做到这一点?

我当前的代码:

class User
  include DataMapper::Resource

  # Some Properties here

  has n, :also_known_as, 'String'
end

这产生的错误是这样的:

irb(main):001:0> require 'cloudsdale'
NoMethodError: undefined method `relationships' for String:Class
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/associations/one_to_many.rb:109:in `finalize'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:782:in `block in finalize_relationships'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `block in each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `block in each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:782:in `finalize_relationships'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:137:in `finalize'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core.rb:281:in `block in finalize'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/descendant_set.rb:64:in `block in each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `block in each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `block in each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/descendant_set.rb:63:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core.rb:281:in `finalize'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/cloudsdale-0.0.1/lib/cloudsdale.rb:19:in `<top (required)>'
    from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
    from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
    from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from (irb):1
    from C:/Ruby193/bin/irb:12:in `<main>'irb(main):002:0>

产生错误的文件是这样的:

# Load in DataMapper
# Change the setup to meet your DB needs
require 'data_mapper'
DataMapper.setup(:default, 'abstract::')

# API objects
require 'cloudsdale/version'
require 'cloudsdale/api/api'
require 'cloudsdale/client'

# Models
require 'cloudsdale/models/user'
require 'cloudsdale/models/cloud'
require 'cloudsdale/models/avatar'
require 'cloudsdale/models/ban'
require 'cloudsdale/models/chat'

# Finalize DataMapper so the models Load
DataMapper.finalize
4

2 回答 2

0

你为什么在'String'这里使用?

has n, :also_known_as, 'String'

没有意义,删除它:

has n, :also_known_as

如果要设置模型,请使用:model选项:

has n, :also_known_as, :model => ModelName

而且我不确定你想String用作模型名称。

很可能您需要一个额外的模型来保留用户以前的名称:

class UserAlias
  include DataMapper::Resource

  # ...
end


class User
  include DataMapper::Resource

  # ...

  has n, :also_known_as, :model => UserAlias
end
于 2012-12-08T10:43:12.460 回答
0

如果您希望能够通过 datamapper 1.x 查询 DSL 搜索较旧的用户名,您需要定义一个附加模型。

class User
  include DataMapper::Resource

  # ...

  has n, :also_known_as, :model => UserNameHistory
end

class UsernameHistory
  include DataMapper::Resource
  property :id, Serial
  property :name
  belongs_to :user
end

如果您不需要通过旧用户名进行查询,您可以使用序列化的大对象。作为建议,您可以像这样使用DataMapper::Property::YAMLfrom dm-types

class User
  include DataMapper::Resource

  # ...

  property :also_known_as, YAML
end
于 2012-12-14T19:32:36.333 回答