English 中文(简体)
积极记录`自_csv' 方法——如何使其更加性化?
原标题:ActiveRecord `from_csv` method - howto make it more sexy?

我已经用书面方法将数据从CSV中输入到DB。 这对我来说,是一小小 ,远离南斯拉夫:

  def self.from_csv(data)
    c = Company.new
    FasterCSV.parse(data) do |row|
      c.name = row[0]
      c.street = row[1]
      c.street_number = row[2]
      c.apartament_number = row[3]

      c.city = row[4]
      c.post_code = row[5]
      c.post_office_city = row[6]

      c.nip = row[7]
      c.regon = row[8]
      c.vatin = row[9]
    end
  end

如何使其更加性化,或者在装上数据方面已经有什么平衡?

最佳回答

http://www.chris.com/ascii/index.php?art=people/naked%20ladies”

#          ___
#   _  _.-"_< }
#    ""--"" 7(
#          /())
#         / )/
#        ^ ( 
#          / /
#         /. 
#        //
# ______/L___ sexii
def self.from_csv(data)
  #...
end

但是,我对你的法典所看到的唯一问题是,你不能轻易地重新安排属性,因为你必须人工更新所有指数。 我更喜欢用一个阵列的名词清单,并使用一些鲁比拉动态方法:

def self.from_csv(data)
  company = new
  row = CSV.parse_line(data)
  [:name, :street, :street_number, :apartament_number,
   :city, :post_code, :post_office_city,
   :nip, :regon, :vatin].each_with_index do |name, i|
    company.send(:"#{name}=", row[i])
  end
  company
end

还指出,您最终必须退回一个经过分析的公司案例,否则,在打上时,你将获得一些随机价值。

问题回答
c = Company.new
cols = ["name", "street", "street_number", "apartament_number", "city", "post_code", "post_office_city", "nip", "regon", "vatic"]

FasterCSV.parse(data) do |row|
  cols.each_index { |i| c.send("#{cols[i]}=", row[i]) }
end

我写了这一轻重包装,装上了CSV文档:

https://github.com/stackpilot/loady

它使用1.9瓦卢,并使用标准卢布的CSV图书馆(前称“快车”)。

你这样做:

Loady.csv "/your/file.csv" do |row|
  Company.create row.to_attributes [:name, :street, :street_number, ...]
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签