Rails rake db:seed插入null而不是值

编程入门 行业动态 更新时间:2024-10-28 17:27:31
本文介绍了Rails rake db:seed插入null而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Ruby 2.1.5和Rails 4.2.1,我正在尝试通过seed.rb将一些静态数据库条目放入sqlite3表中.当我运行rake db:seed时,我获得了插入正确的行数以及相应的时间戳列,但是没有填充实际的数据列name.名称正在循环内打印出来.

I'm using Ruby 2.1.5 and Rails 4.2.1 I'm trying to put some static database entries into a sqlite3 table via seeds.rb. When I run rake db:seed, I get the correct number of rows inserted with appropriate timestamp columns, but the actual data column, name, is not being populated. Name is being printed out inside the loop.

db/seed.rb

db/seed.rb

for g in ['Harmony', 'Melody', 'Technique', 'Soloing'] Group.create(name: g) put(g) end

app/models/group.rb:

app/models/group.rb:

class Group < ActiveRecord::Base attr_accessor :name has_many :group2exercise has_many :exercises, through :group2exercise end

sqlite3(从SQLDB浏览器复制创建的内容)

sqlite3 (copying the create from SQLDB Browser)

CREATE TABLE "groups"("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)

推荐答案

如果删除该行,它应该可以工作

It should work if you remove the line

attr_accessor :name

通过具有attr_accessor,将创建​​一组新的getter和setter方法,在这种情况下,它们将覆盖Rails提供的内容.

By having attr_accessor, a new set of getter and setter methods are created, in this case overriding what Rails provided.

更多推荐

Rails rake db:seed插入null而不是值

本文发布于:2023-10-22 20:53:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   rake   Rails   db   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!