小编ben*_*ang的帖子

如何将Mongoid foreign_key保存为整数或保持父模型具有Integer id

情况就是这样.

user embed_one profile
profile belongs_to city
Run Code Online (Sandbox Code Playgroud)

我填写了一张城市表

id as Integer
name as String
Run Code Online (Sandbox Code Playgroud)

现在我正在 user.update_attributes(:profile_attributes{:city_id=>"5"})模拟浏览器表单提交.然后我检查user.profile我看到city_id存储为字符串.这使我的user.profile.city命令为零.

我想知道在这里做什么是正确的.我应该让我的城市ID是字符串还是BSON对象?或者我应该尝试拦截update_attributes以使mongoid存储city_id为整数?我使用Integer作为城市id的原因是因为我认为通过Integer搜索比搜索字符串更快.而且我还有状态和城市表格,我希望以可预测的方式匹配ID,所以我不想使用BSON randome密钥.

ruby mongoid ruby-on-rails-3

12
推荐指数
1
解决办法
330
查看次数

在Ruby on Rails中验证Django密码会提供不匹配的密码

我正在重写Ruby on Rails中的Django应用程序,并希望保留用户的旧密码.

Django使用PBKDF2SHA1作为加密机制.所以我有一个加密密码就是这个

pbkdf2_sha256$10000$YsnGfP4rZ1IZ$Tpf4922MoNEjuJQA9EG2Elptyt3dMAyzBPUgmunFOW4=
Run Code Online (Sandbox Code Playgroud)

原始密码是 2bulls

在Ruby中,我使用PBKDF256 gem和base64进行检查.

Base64.encode64 PBKDF256.dk("2bulls", "YsnGfP4rZ1IZ", 10000, 32)
Run Code Online (Sandbox Code Playgroud)

我期待着

Tpf4922MoNEjuJQA9EG2Elptyt3dMAyzBPUgmunFOW4=
Run Code Online (Sandbox Code Playgroud)

但是,我得到了

YEfK6oUGFHdaKZMDXC0Dz8TpwsJlKfqC5vjCxjo+ldU=
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

UPDATE

如果它对你更有意义,可以在django源代码中找到代码.

class PBKDF2PasswordHasher(BasePasswordHasher):
    """
    Secure password hashing using the PBKDF2 algorithm (recommended)

    Configured to use PBKDF2 + HMAC + SHA256 with 10000 iterations.
    The result is a 64 byte binary string.  Iterations may be changed
    safely but you must rename the algorithm if you change SHA256.
    """
    algorithm = "pbkdf2_sha256"
    iterations = 10000
    digest = hashlib.sha256

    def encode(self, password, salt, …
Run Code Online (Sandbox Code Playgroud)

ruby encryption django ruby-on-rails

6
推荐指数
1
解决办法
993
查看次数

如果mixin方法名称冲突,如何选择要调用的方法?

当您在具有方法名称冲突的类中包含模块时,它将使用该类定义的方法.有没有办法选择我想要运行哪一个?

module B
  def self.hello
    "hello B"
  end
end

class A
  include B
  def self.hello
     "hello A"
  end
end

A.hello #=> this prints "hello A", what if I want "hello B"?
Run Code Online (Sandbox Code Playgroud)

ruby

5
推荐指数
2
解决办法
3402
查看次数