Faker种子数据编码错误
Faker seed data coding error
我是一名新 rails 学生,正在从事一个有用户登录的项目,每个用户都有一个列表,每个列表有很多项目。项目嵌套在我的路线列表下。
我遇到了一些问题。我下面的代码加载了一个用户和该用户的一个列表,但无法加载列表的项目。我的想法是首先让它工作,但最后我想要有 5 个用户,每个用户有一个列表,每个列表上的 15 个项目都是由 faker 创建的。感谢任何帮助。
require 'faker'
# Create an admin user
admin = User.new(
name: 'Admin User',
email: 'admin@example.com',
password: 'helloworld'
)
admin.skip_confirmation!
admin.save!
# Create List
1.times do
List.create(
user: admin,
title: 'Admin List'
)
end
# Create Items
15.times do
Item.create(
list: 'Admin List',
name: Faker::Company.bs
)
end
puts "Seed finished"
puts "#{User.count} users created"
puts "#{List.count} lists created"
puts "#{Item.count} items created"
这是完整的错误
heidi-golds-imac:blocitoff dale$ rake db:seed
rake aborted!
ActiveRecord::AssociationTypeMismatch: List(#70281090559180) expected, got String(#70281054267240)
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:216:in `raise_on_type_mismatch!'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/belongs_to_association.rb:12:in `replace'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/singular_association.rb:17:in `writer'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/associations/builder/association.rb:123:in `list='
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `public_send'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:35:in `each'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in `init_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in `initialize'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:33:in `create'
/Users/dale/code/blocitoff/db/seeds.rb:22:in `block in <top (required)>'
/Users/dale/code/blocitoff/db/seeds.rb:21:in `times'
/Users/dale/code/blocitoff/db/seeds.rb:21:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.0/gems/railties- 4.2.0/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
物品控制器
class ItemsController < ApplicationController
before_action :find_list
def create
@item = @list.items.build(params.require(:item).permit(:name))
unless @item.save
flash[:error] = "There was an error. Please try again."
end
redirect_to @list
end
private
def find_list
@list = List.find(params[:list_id])
end
end
列出控制器
class ListsController < ApplicationController
before_action :authenticate_user! # users must be signed in before any lists_controller method
def show
@list = current_user.list
end
def new
@list = List.new
end
def create
@list = current_user.build_list(params.require(:list).permit(:title, :body))
if @list.save
flash[:notice] = "List was saved."
redirect_to @list
else
flash[:error] = "There was an error. Please try again."
render :new
end
end
def edit
@list = List.find(params[:id])
end
def update
@list = List.find(params[:id])
if @list.update_attributes(params.require(:list).permit(:title, :body))
flash[:notice] = "List was updated."
redirect_to @list
else
flash[:error] = "There was an error. Please try again."
render :edit
end
end
end
尝试更正以下代码:
# Create Items
15.times do
Item.create(
list: 'Admin List'
name: Faker::Company.bs
)
end
只需在列表后添加一个逗号:'Admin List'
如果这不能解决问题,请 post 完整的错误消息并指定您收到错误的行。
问题是因为您是按名称而不是按引用将列表分配给项目。在创建列表的代码块中,您需要将其分配给一个变量:
列表=List.create...,
然后,在创建项目时,您需要执行以下操作:
Item.create(名单:名单....
您正在使用列表的标题,它正在寻找列表 object。
我是一名新 rails 学生,正在从事一个有用户登录的项目,每个用户都有一个列表,每个列表有很多项目。项目嵌套在我的路线列表下。
我遇到了一些问题。我下面的代码加载了一个用户和该用户的一个列表,但无法加载列表的项目。我的想法是首先让它工作,但最后我想要有 5 个用户,每个用户有一个列表,每个列表上的 15 个项目都是由 faker 创建的。感谢任何帮助。
require 'faker'
# Create an admin user
admin = User.new(
name: 'Admin User',
email: 'admin@example.com',
password: 'helloworld'
)
admin.skip_confirmation!
admin.save!
# Create List
1.times do
List.create(
user: admin,
title: 'Admin List'
)
end
# Create Items
15.times do
Item.create(
list: 'Admin List',
name: Faker::Company.bs
)
end
puts "Seed finished"
puts "#{User.count} users created"
puts "#{List.count} lists created"
puts "#{Item.count} items created"
这是完整的错误
heidi-golds-imac:blocitoff dale$ rake db:seed
rake aborted!
ActiveRecord::AssociationTypeMismatch: List(#70281090559180) expected, got String(#70281054267240)
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:216:in `raise_on_type_mismatch!'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/belongs_to_association.rb:12:in `replace'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/singular_association.rb:17:in `writer'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/associations/builder/association.rb:123:in `list='
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `public_send'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:35:in `each'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in `init_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in `initialize'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:33:in `create'
/Users/dale/code/blocitoff/db/seeds.rb:22:in `block in <top (required)>'
/Users/dale/code/blocitoff/db/seeds.rb:21:in `times'
/Users/dale/code/blocitoff/db/seeds.rb:21:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.0/gems/railties- 4.2.0/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
物品控制器
class ItemsController < ApplicationController
before_action :find_list
def create
@item = @list.items.build(params.require(:item).permit(:name))
unless @item.save
flash[:error] = "There was an error. Please try again."
end
redirect_to @list
end
private
def find_list
@list = List.find(params[:list_id])
end
end
列出控制器
class ListsController < ApplicationController
before_action :authenticate_user! # users must be signed in before any lists_controller method
def show
@list = current_user.list
end
def new
@list = List.new
end
def create
@list = current_user.build_list(params.require(:list).permit(:title, :body))
if @list.save
flash[:notice] = "List was saved."
redirect_to @list
else
flash[:error] = "There was an error. Please try again."
render :new
end
end
def edit
@list = List.find(params[:id])
end
def update
@list = List.find(params[:id])
if @list.update_attributes(params.require(:list).permit(:title, :body))
flash[:notice] = "List was updated."
redirect_to @list
else
flash[:error] = "There was an error. Please try again."
render :edit
end
end
end
尝试更正以下代码:
# Create Items
15.times do
Item.create(
list: 'Admin List'
name: Faker::Company.bs
)
end
只需在列表后添加一个逗号:'Admin List' 如果这不能解决问题,请 post 完整的错误消息并指定您收到错误的行。
问题是因为您是按名称而不是按引用将列表分配给项目。在创建列表的代码块中,您需要将其分配给一个变量:
列表=List.create...,
然后,在创建项目时,您需要执行以下操作:
Item.create(名单:名单....
您正在使用列表的标题,它正在寻找列表 object。