Ruby on Rails Error

+1 Tim Bogdanov · June 24, 2015
So im following along with some tutorials but Mackenzie Child on youtube and i stumbled up on an error. this is how it goes.


In my user.rb file i have the association of the user -> links
# app/models/user.rb
has_many :links


And my link.rb i have the association on the links -> users (as i understood)

# app/models/link.rb
belongs_to :user



when i go to to check if there is a link between them with 
@link.user

it throws an error:

NoMethodError: undefined method `user' for nil:NilClass
from (irb):6
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:110:in `start'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:9:in `start'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands.rb:17:in `'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
from /Users/92slavik/Sites/reddit/bin/rails:8:in `'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /Users/92slavik/.rvm/gems/ruby-2.2.1/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `'
from /Users/92slavik/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/92slavik/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'


and if i just do:
2.2.1 :008 > @link


it gives a 
=> nil

Post a Reply

Replies

Oldest  Newest  Rating
+2 Nikola Novakovic · July 22, 2015
Make sure you have in links table a foreign key reference to a user. You can do that via migration, something like this:
$ rails g migration add_user_id_to_links #this generates migration file in db/migrate


#db/migrate/222000222_add_user_id_to_links.rb number after migrations/ will be different for you

class NameOfMigration < ActiveRecord::Migration

def change
add_column :links, :user_id
end

end


Now the relation should work. In what you had before even tough you have setup everything on a Model level you did not have the relationship down on a db level. 
Let me know if you still have some problems.
0 Tim Bogdanov · July 22, 2015
Got it thanks :)
  • 1

Ruby

107,281 followers
About

Write web apps quickly and easily with this easy to learn language.

Links
Moderators
Bucky Roberts Administrator