I’m a new rails student working on a project that has a user login, each user has one list and each list has many items. Items are nested under lists in my routes.
I’m having a few issues. My code below loads one user and one list for that user, but fails to load items for the list. My thought was to get this to work first, but in the end I wanted to have 5 users, each with one list and 15 items on each list all created by faker. Any help is appreciated.
require 'faker'
# Create an admin user
admin = User.new(
name: 'Admin User',
email: '[email protected]',
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"
This is the full error
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)
Items Controller
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
Lists Controller
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
The issue is because you are assigning the list to the item by name rather than by reference. In the block of code where you create your list, you need to assign that to a variable:
list = List.create...,
Then, when creating your item, you need to do this:
Item.create(list: list....
You were using the title of the list, where it was looking for a list object.