我想在两个表上进行外连接,然后选择某些列并将所有行写入CSV。似乎属于Article表的属性按预期添加到row_container
但是Author表(author.name
)的属性抛出了NoMethodError,我假设因为ruby正在寻找一个名为author.name
。任何人都可以提供这方面的任何提示吗?
require 'csv'
data = Article.joins('LEFT OUTER JOIN authors ON author_id = authors.id')
columns = ["title", "date", "author.name"]
CSV.open("output.csv", "w") do |csv|
csv << columns
data.each do |row|
row_container = []
columns.each {|i| row_container << row.send(i) }
csv << row_container
end
end