小编ele*_*ify的帖子

使用 uuid-ossp 将 Rails 中的模型 id 更改为 uuid

我需要将现有模型的 id 更改为 uuid。我正在使用本指南:

http://rny.io/rails/postgresql/2013/07/27/use-uuids-in-rails-4-with-postgresql.html

但不知道如何使下面的迁移适应变化(而不是创建新的迁移):

 class CreateDocuments < ActiveRecord::Migration
  def change
   create_table :documents, id: :uuid  do |t|
      t.string :title
      t.string :author
      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

uuid ruby-on-rails rails-migrations

3
推荐指数
1
解决办法
3629
查看次数

Ruby:阵列数组到哈希数组

我有一个数组(从CSV文件导入):

[[title1],[title2],[title3],[title4],[title5]],
[[song1],[author1],[bpm1],[key1],[energy1]],
...
[[song100],[author100],[bpm100],[key100],[energy100]].
Run Code Online (Sandbox Code Playgroud)

并希望将其转换为哈希数组,如:

[{"title1"=>"song1","title2"=>"author1","title3"=>"bpm1","title4"=>"key1","title5"=>"energy1"}],
...
[{"title1"=>"song100","title2"=>"author100","title3"=>"bpm100","title4"=>"key100","title5"=>"energy100"}].
Run Code Online (Sandbox Code Playgroud)

我使用下面的代码,但它不起作用:

    require 'csv'
    csv=CSV.read('library.csv')

array_hash=[]
hash={}

for i in 1..(csv.size)

  hash1={}

    for n in 0..4

        a=csv[0][n]
        b=csv[i][n]
        hash1[a]=b
        hash.merge!(hash1)

    end

  array_hash.push(hash)

end
Run Code Online (Sandbox Code Playgroud)

但我得到:

> NoMethodError: undefined method `[]' for nil:NilClass     from
> (irb):149:in `block (2 levels) in irb_binding'    from (irb):146:in
> `each'    from (irb):146:in `block in irb_binding'    from (irb):143:in
> `each'    from (irb):143  from
> /Users/user/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
Run Code Online (Sandbox Code Playgroud)

这有什么问题?如何使用.each做同样的事情?

ruby csv arrays hash

2
推荐指数
1
解决办法
99
查看次数

Rails中的Active Record OR查询4

我需要在Event对象的2列中找到用户ID,因此需要使用OR查询.这样做的正确方法是什么?下面的代码不起作用.

@event=Event.where((user_id= (current_user.id)) or (user2_id= current_user.id))
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails

0
推荐指数
1
解决办法
83
查看次数

标签 统计

ruby ×2

ruby-on-rails ×2

activerecord ×1

arrays ×1

csv ×1

hash ×1

rails-migrations ×1

uuid ×1