Rails:使用文本文件中的数据填充模型

时间:2021-08-13 09:46:46

I am new to Rails, and so far, I have been filling model's data from the standard HTML form. What if I want to have a model called "Car" with attributes "brand", and "color" and I want to read 6 cars BUT from a text file (not an HTML form) and create 6 different car models? I have to idea where to start from. Any guidance will be very useful. Thanks

我是Rails的新手,到目前为止,我一直在填写标准HTML表单中的模型数据。如果我想要一个名为“Car”的模型,其属性为“品牌”和“颜色”,我想从文本文件(不是HTML表单)中读取6辆车,并创建6种不同的车型,该怎么办?我不得不知道从哪里开始。任何指导都非常有用。谢谢

2 个解决方案

#1


1  

1) If it's for when the app is installed put Model.create statements in db/seeds.rb

1)如果是安装应用程序的时候,请在db / seeds.rb中放入Model.create语句

2) For what are effectively model level constants, put them in classes, e.g.

2)对于什么是有效的模型级常量,将它们放在类中,例如

class Car < ActiveRecord::Base
  MODELS = %q[Ford, Honda, Buick] # %q means words, e.g. "Ford", "Honda", "Buick"
end

# Now you can use `::` to access it, e.g. Car::MODELS for the car models array.

2a) You can also create them through references, e.g.

2a)您也可以通过引用创建它们,例如

class Car < ActiveRecord::Base
  FORD = "The Ford Motor Company."
  HONDA = "The international Honda Motor Company."
  BUICK = "Buick Inc."
  MODELS = [FORD, HONDA, BUICK]
end

# Now you can use Book::MODELS for the models array.
# and Car::FORD for the FORD type

You can edit classes at any time (this took me a bit of getting used to as I was a SQL for everything programmer for a while).

您可以随时编辑类(这让我有点习惯,因为我曾经是一段时间的程序员的SQL)。

3) Files themsevlves can be easily read with

3)文件可以很容易地阅读

@input = File.open("/directories_to_it/file")
@input.each_line do |one_row|
  CarBrand.create(:brand => one_row[0], :color => one_row[1]
end
# psuedo-code you may need to play with the line reading a bit for your columns.

#2


0  

JSON is a great object representational format that is easy to use and is human readable. It can handle arrays of objects as you describe. I would convert your object(s) to json and write them to a file, which you can then retrieve using JSON.parse().

JSON是一种很好的对象表示格式,易于使用且易于阅读。它可以像你描述的那样处理对象数组。我会将你的对象转换为json并将它们写入一个文件,然后你可以使用JSON.parse()来检索它。

see this link for an example of how to do this: How to write to a JSON file in the correct format

请参阅此链接以获取如何执行此操作的示例:如何以正确的格式写入JSON文件

#1


1  

1) If it's for when the app is installed put Model.create statements in db/seeds.rb

1)如果是安装应用程序的时候,请在db / seeds.rb中放入Model.create语句

2) For what are effectively model level constants, put them in classes, e.g.

2)对于什么是有效的模型级常量,将它们放在类中,例如

class Car < ActiveRecord::Base
  MODELS = %q[Ford, Honda, Buick] # %q means words, e.g. "Ford", "Honda", "Buick"
end

# Now you can use `::` to access it, e.g. Car::MODELS for the car models array.

2a) You can also create them through references, e.g.

2a)您也可以通过引用创建它们,例如

class Car < ActiveRecord::Base
  FORD = "The Ford Motor Company."
  HONDA = "The international Honda Motor Company."
  BUICK = "Buick Inc."
  MODELS = [FORD, HONDA, BUICK]
end

# Now you can use Book::MODELS for the models array.
# and Car::FORD for the FORD type

You can edit classes at any time (this took me a bit of getting used to as I was a SQL for everything programmer for a while).

您可以随时编辑类(这让我有点习惯,因为我曾经是一段时间的程序员的SQL)。

3) Files themsevlves can be easily read with

3)文件可以很容易地阅读

@input = File.open("/directories_to_it/file")
@input.each_line do |one_row|
  CarBrand.create(:brand => one_row[0], :color => one_row[1]
end
# psuedo-code you may need to play with the line reading a bit for your columns.

#2


0  

JSON is a great object representational format that is easy to use and is human readable. It can handle arrays of objects as you describe. I would convert your object(s) to json and write them to a file, which you can then retrieve using JSON.parse().

JSON是一种很好的对象表示格式,易于使用且易于阅读。它可以像你描述的那样处理对象数组。我会将你的对象转换为json并将它们写入一个文件,然后你可以使用JSON.parse()来检索它。

see this link for an example of how to do this: How to write to a JSON file in the correct format

请参阅此链接以获取如何执行此操作的示例:如何以正确的格式写入JSON文件