Ruby on Rails Active Record创建:无方法错误

时间:2015-02-03 10:37:59

标签: ruby-on-rails rails-activerecord

使用Ruby on Rails完成我的第一步,我正在尝试使用活动记录创建我的数据库模型。 现在,我已经这样做了:

rails generate model seminar title 
rails generate model student firstname lastname

比我运行rake db:migrate并编辑.rb文件,如下所示:

class Seminar < ActiveRecord::Base
has_many :students
end

class Student < ActiveRecord::Base
belongs_to :seminar
end

之后,我在rails控制台中创建了一个新的Seminar-Object:

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

现在我想创建一个新的Student-Object:

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

此时,我收到以下错误:

(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 > 

我无法弄清楚问题。

我也想知道是否需要编辑这两个类(has_many / belongs_to)或者这是多余的。

2 个答案:

答案 0 :(得分:2)

学生表中应该有seminar_id作为外键 所以运行命令 - &gt;

rails g migration add_seminar_id_to_students seminar_id:integer

然后做

rake db:migrate

答案 1 :(得分:1)

我假设您有以下内容: -

  1. 两种型号.. seminar.rbstudent.rb
  2. 带有迁移的两个表格seminarsstudents(带seminar_id列)
  3. 因为当你说seminars.students.create()时...你链接两个表 ..后来由seminar_id表格中的foreign_key students .... / p>