Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails _csv的续集使用哈希而不是数据生成文件_Ruby On Rails_Ruby_Ruby On Rails 4_Sequel - Fatal编程技术网

Ruby on rails _csv的续集使用哈希而不是数据生成文件

Ruby on rails _csv的续集使用哈希而不是数据生成文件,ruby-on-rails,ruby,ruby-on-rails-4,sequel,Ruby On Rails,Ruby,Ruby On Rails 4,Sequel,每次我使用Sequel的保存到_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:0x0000000

每次我使用Sequel的
保存到_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>'

我检查了我的续集版本4.28.0是否已弃用
to_csv
,因此我不明白为什么要
to_csv
提供Ruby符号输出。

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

一个小例子:

require 'csv'

class A
end

puts [A.new, A.new].to_csv
结果是:

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

a,a
在您的情况下,这样做会有所帮助:

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

到_csv_文件
是在
顺序中定义的,因此您可以尝试
@Order。到_csv_文件
您的问题不在于
到_csv
本身,而在于模型

一个小例子:

require 'csv'

class A
end

puts [A.new, A.new].to_csv
结果是:

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

a,a
在您的情况下,这样做会有所帮助:

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

to_csv_文件
是在
顺序中定义的,因此您可以尝试
@Order。to_csv_文件

欢迎使用堆栈溢出。虽然它不是重复的,但应该很有用。欢迎使用堆栈溢出。虽然它不是复制品,但应该很有用。