Rails rake db:seed Inserts nulls instead of values

♀尐吖头ヾ 提交于 2019-12-25 12:41:09

问题


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

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

app/models/group.rb:

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

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)

回答1:


It should work if you remove the line

attr_accessor :name

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



来源:https://stackoverflow.com/questions/34123665/rails-rake-dbseed-inserts-nulls-instead-of-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!