【问题标题】:Sequel to_csv generates file with hash instead of data续集 to_csv 生成带有哈希而不是数据的文件
【发布时间】:2016-02-05 04:46:20
【问题描述】:

每次我使用 Sequel 的 to_csv 保存时,它都会在 CSV 中为我提供如下对象:

2.1.0 :033 > @order.radcheck.to_csv
 => "#<Radcheck:0x0000000697bb38>,#<Radcheck:0x0000000697ba20>\n" 
2.1.0 :034 > @orders.to_csv
 => "#<Order:0x000000069ee4d0>,#<Order:0x000000069ee318>,#<Order:0x000000069ee110>,#<Order:0x000000069ede68>,#<Order:0x000000069edc38>,#<Order:0x000000069edaa8>\n" 
2.1.0 :035 > @order.radcheck.class
 => Array 
2.1.0 :036 > @orders.class
 => Array

2.1.0 :038 > @order.radcheck
 => [#<Radcheck @values={:id=>2, :username=>"19cw5212443948", :attribute=>"User-Password", :op=>":=", :value=>"R65B60178yS", :order_id=>2}>, #<Radcheck @values={:id=>3, :username=>"D83d6raf47", :attribute=>"User-Password", :op=>":=", :value=>"90S4449y7845", :order_id=>2}>]

2.1.0 :039 > @order.radcheck.to_csv_file
NoMethodError: undefined method `to_csv_file' for #<Array:0x00000006981150>
    from (irb):39
    from /var/www/quadifi_staging/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
    from /var/www/quadifi_staging/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
    from /var/www/quadifi_staging/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /var/www/quadifi_staging/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /var/www/quadifi_staging/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'

另外,我创建了一个自定义的to_csv_file 方法,但这不起作用。生成的 CSV 文件将是这样的。

class Order < Sequel::Model
  plugin :validation_helpers
  plugin :nested_attributes
  plugin :timestamps, :update_on_create => true
  many_to_one :user
  one_to_many :radcheck

  nested_attributes :radcheck

  def self.to_csv_file(options = {})
    CSV.generate(options) do |csv|
      csv.add_row column_names
      @order.radcheck.all.each do |foo|
        values = foo.attributes.values
        csv.add_row values
      end
    end
  end

  def validate
    super
    validates_presence [ :time_limit, :ticket_count ]
  end
end


Class Radcheck < Sequel::Model(:radcheck)
  plugin :nested_attributes

  one_to_one :radusergroup
  many_to_one :user

  nested_attributes :radusergroup

end

我检查了我的 Sequel 版本 4.28.0 已弃用 to_csv,所以我很困惑为什么我有 to_csv 提供 Ruby 符号输出。

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-4 sequel


【解决方案1】:

你的问题不是to_csv本身,而是模型。

一个小例子:

require 'csv'

class A
end

puts [A.new, A.new].to_csv

结果:

#<A:0x00000002fa0148>,#<A:0x00000002fa00d0>

to_csv 获取数组的每个元素并将字符串表示添加到 csv 行。

如果你用to_s 定义另一个字符串表示,你会得到更好的结果。

例子:

require 'csv'

class A
  def to_s
    'a'
  end
end

puts [A.new, A.new].to_csv

结果:

a,a

在你的情况下,这样的事情会有所帮助:

Class Radcheck < Sequel::Model(:radcheck)
  #...
  def to_s
    "Radcheck:..." #Add here some attributes of your table
  end
end

您必须将 "Radcheck:..." 替换为要添加到 csv 中的一些 dato。


关于你的问题

2.1.0 :039 > @order.radcheck.to_csv_file
NoMethodError: undefined method `to_csv_file' for #<Array:0x00000006981150>

to_csv_file 是在Order 中定义的,所以你可以试试@order.to_csv_file

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 2012-11-19
    • 2016-07-16
    • 2011-12-16
    • 2019-04-28
    • 2011-06-08
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多