【发布时间】: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 符号输出。
【问题讨论】:
-
欢迎来到 Stack Overflow。虽然它不是重复的,但stackoverflow.com/q/16592661/128421 应该很有用。
标签: ruby-on-rails ruby ruby-on-rails-4 sequel