Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 4 在rails中创建或更新记录_Ruby On Rails 4_Updating - Fatal编程技术网

Ruby on rails 4 在rails中创建或更新记录

Ruby on rails 4 在rails中创建或更新记录,ruby-on-rails-4,updating,Ruby On Rails 4,Updating,我正在从外部数据库读取数据。 因此,我这样做是为了在代码第一次运行时创建数据库。 我只粘贴创建部分。从外部数据库读取的代码是我不允许公开的 Collection.create( name: xxxxxx, title: xxxxxx, description: xxxxx, date: xxxxx, collectie: xxxxx, colors

我正在从外部数据库读取数据。 因此,我这样做是为了在代码第一次运行时创建数据库。 我只粘贴创建部分。从外部数据库读取的代码是我不允许公开的

Collection.create(
        name:         xxxxxx,
        title:        xxxxxx,
        description:  xxxxx,
        date:         xxxxx,
        collectie:    xxxxx,
        colors:       xxxxxx,
        small_image:  xxxxxx,
    )
我怎样才能更改它,以便下次在数据更改的地方只更新记录,这样我就不必执行rake:drop和rake db:migrate,然后执行这个rake任务,如果我想获得最新的数据

编辑1:我试图更改以查找或创建,但后来我看到了很多错误

item = Collection.where(
    name:         xxxxxx,
    title:        xxxxxx,
    description:  xxxxx,
    date:         xxxxx,
    collectie:    xxxxx,
    colors:       xxxxxx,
    small_image:  xxxxxx
).first_or_create

然后,您可以使用item.update操作项目。如果需要,请更新。

谢谢,我尝试过,但看到了此错误消息;看起来您的模型有问题。集合是否与数据库中的列匹配?