Rails rake db:seed插入null而不是值 [英] Rails rake db:seed Inserts nulls instead of values

查看:98
本文介绍了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而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆