问题
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