我正在使用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) endapp/models/group.rb:
app/models/group.rb:
class Group < ActiveRecord::Base attr_accessor :name has_many :group2exercise has_many :exercises, through :group2exercise endsqlite3(从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而不是值
发布评论