Reputation: 678
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.
Upvotes: 1
Views: 1671
Reputation: 2575
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
Upvotes: 2
Reputation: 5112
i assume you have the following:-
seminar.rb
and student.rb
seminars
and students
(with seminar_id
column) with migrationsbecause when you say seminars.students.create()
...you are linking two tables ..the later by foreign_key seminar_id
in students
table....
Upvotes: 1