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

Jason Chow
Jason Chow
4,428 Points

no method error 'belong_to'. why?

require 'spec_helper'

describe TodoItem do

it { should belong_to(:todo_list) }

end

6 Answers

Andrew Winkler
Andrew Winkler
37,739 Points

Per the shoulda-matchers docs, you need to add some configuration to your spec_helper.rb. I looked back at my odot, I had shoulda-matchers 2.6.0 and no configuration. This must be new in 3.0. Anyways, you will need:

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

Add that to the end of spec_helper.rb. (Outside of the rspec configure block.) You should get a pass now.

Alexis Leblanc-Isabelle
Alexis Leblanc-Isabelle
9,558 Points

Thanks for your help. Can you explain briefly what is this code and how did you came with that ? :)

Matthew Timmons
Matthew Timmons
33,804 Points

Thank you! This has been killing me.

Andrew Winkler
Andrew Winkler
37,739 Points

Okay, what I love and hate about Ruby is that the commonly used term of "Ruby magic" means the speaker is arbitrarily judging that it's of no worth to explain what's actually going on. This leads to gaps in understanding, etc...

One of the things Ruby is awesome at doing is providing a data management system for querying. Other languages like PHP will use SQL commands integrated into PHP code. -- Note: look up these languages if you don't know what they are! Otherwise you look like a schmuck when they come up in conversation. I'm speaking from experience.

Rspec is one of the magical Ruby gems (aka fancy name for mod/package) that has become very popular to test the logic of your executable Ruby code between the integrated database of the Ruby framework. Ruby is an executable language not a database. The database is built into its framework, and the industry standard SQL language has been simplified and represented by differently named ruby functions. SQL widely considered a pain in the ***. Similar to accounting, it often drives people to drink. But have no fear! Ruby magic to the rescue!!!

The wrapped SQL database in the ruby framework is still a SQL database, which means that by nature it does not feature an undo button. This means if you screw up the database running edge case tests, you'd be screwed. Rspec protects against this though by creating a clone of the database (test_framework) for running your crazy tests on. The code block above is the configuration step to do exactly that. You can look up the logic of each function, but essentially it is setting up the test environment for rspec to operate upon. The mentioned code block loops through rows and columns of the SQL database (which you can think of as an Excel spreadsheet) then it clones them into the test_framework. This test_framework is created and destroyed every time you run rspec -- it does not permanently exist to store data. Which is nice because personally I have limited space on my hard drive. If there's a logical error any data between your executable ruby code and the database, rspec will kick back an error. Then after you modify/fix your executable code. Then the cloning/testing/deleting process begins all over again.

It's a lot to swallow, but does that make sense?

Jason Chow
Jason Chow
4,428 Points

i got a pass not only for todo_item, but todo_list as well! thanks!! Do you mind explaining what that code block does? Why do i need to add it and why must it be in 'spec_helper.rb'? im still confused about the relationships and roles among the files in my app. thanks Andrew!

Tijs Limburg
Tijs Limburg
5,098 Points

Thanks, great answers!

Todd MacIntyre
Todd MacIntyre
12,248 Points

You can also simply roll back your version of shoulda matchers to that which is used in Jason's video. Put this into your gemfile in the group :test section:

gem 'shoulda-matchers', '~> 2.4.0'

and then re-bundle your gemfiles by going into the odot directory in your console and entering:

bundle

This fixed the problem for me. But I have also tried the solution mentioned above and that works as well. Good luck!