1

Doing my first steps with Ruby on Rails, I am trying to create my database model with active records. For now, I have done this:

rails generate model seminar title 
rails generate model student firstname lastname

Than I ran rake db:migrate and edited the .rb files like this:

class Seminar < ActiveRecord::Base
has_many :students
end

class Student < ActiveRecord::Base
belongs_to :seminar
end

After that, I created a new Seminar-Object in rails console:

 seminar = Seminar.create(title: 'The first')

Now I want to create a new Student-Object:

seminar.students.create(firstname: 'Peter', lastname: 'Pan')

And at this point, I get the following error:

(0.0ms)  begin transaction
(0.1ms)  rollback transaction
NoMethodError: undefined method `val' for #<Arel::Nodes::BindParam:0x000000057b28c0>
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:572:in `block (2 levels) in where_values_hash'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:568:in `fetch'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:568:in `block in where_values_hash'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:566:in `map'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:566:in `where_values_hash'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:579:in `scope_for_create'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/collection_association.rb:503:in `create_scope'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:168:in `initialize_attributes'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:248:in `block in build_record'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:282:in `initialize'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:131:in `build_association'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:247:in `build_record'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/collection_association.rb:489:in `block in _create_record'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/collection_association.rb:173:in `block in transaction'
... 11 levels...
from /home/izb/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /home/izb/Development/railstests/modeltest/bin/rails:8:in `<top (required)>'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/izb/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /home/izb/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/izb/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.2.0 :003 > 

And I can't figure out the problem.

Also I wonder if I need to edit both classes (has_many / belongs_to) or if this is redundant.

2
  • 1
    you need another migration for the foreign key Commented Feb 3, 2015 at 10:47
  • see stackoverflow.com/a/28297348/687142 sontya's answer Commented Feb 3, 2015 at 11:08

2 Answers 2

2

There should be seminar_id as foreign key in Student table So run command ->

rails g migration add_seminar_id_to_students seminar_id:integer

then do

rake db:migrate

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that did it. I thought the respective field (seminar_id) would be added automatically when I edit the classes with belongs_to / has_many;)
1

i assume you have the following:-

  1. Two models..seminar.rb and student.rb
  2. Two tables ..seminars and students(with seminar_id column) with migrations

because when you say seminars.students.create()...you are linking two tables ..the later by foreign_key seminar_id in students table....

1 Comment

Thats right, When I look to my database, I see that rails created these two tables, although there are no foreign keys in them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.