Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Relationships

NoMethodError: undefined method `has_many?'

Execute all commands from lecture, but tests fail. Error message

NoMethodError: undefined method `belongs_to?' - for item and

NoMethodError: undefined method `has_many?' - for list

Models:

file todo_item.rb contains

class TodoItem < ActiveRecord::Base
  belongs_to :todo_list
end

file todo_list.rb contains

class TodoList < ActiveRecord::Base
    has_many :todo_items

  validates :title, presence: true
  validates :title, length: {minimum: 3}
  validates :description, presence: true
  validates :description, length: {minimum: 5}
end

What file has has_many? in your code? I don't believe that's a valid Ruby method.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Can you give us your test code? Do you have shoulda-matchers gem in your Gemfile?

Yes, I have shoulda-matchers - all previous tests are successful.

test file for items

require 'spec_helper'

describe TodoItem do

  it { should belong_to(:todo_list) }

end

test file for todo list

require 'spec_helper'

describe TodoList do
  it { should have_many(:todo_items) }
end

4 Answers

Thank you, it helped.

Sahil Gulati
Sahil Gulati
11,592 Points

Followed the instructions and

Updated: gem 'shoulda-matchers', require: false (in Gemfile) and Added: require 'shoulda-matchers' (in spec/spec_helper.rb)

But still getting the same error

$ bin/rspec spec/models/todo_item_spec.rb 
F

Failures:

  1) TodoItem 
     Failure/Error: it { should belong_to(:todo_list) }
     NoMethodError:
       undefined method `belong_to' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcd5d539918>
     # ./spec/models/todo_item_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.00199 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/models/todo_item_spec.rb:5 # TodoItem 
Adam Sumner
Adam Sumner
36,695 Points

There has been a recent change to the shoulda-matchers gem since version 3 was released. The following solution worked for me:

In the gemfile add in the gem as follows:

group :test do
    gem 'capybara', '~> 2.1.0'
    gem 'shoulda-matchers', '~> 3.0'
end

Then at the bottom of the spec_helper file add:

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
Suprasanna Mishra
Suprasanna Mishra
2,730 Points

This worked great :) Thanks Adam Sumner!

ignacio palma
ignacio palma
8,698 Points

Me too! same problem, please help!

Adam Sumner's solution worked for me.