我有两个型号
bcase.rb
class Bcase < ActiveRecord::Base
belongs_to :pimp
has_many :c_entries, :dependent => :destroy
has_many :s_entries, :dependent => :destroy
accepts_nested_attributes_for :c_entries, :s_entries
validates_uniqueness_of :pimp_id
end
c_entry.rb
class CEntry < ActiveRecord::Base
belongs_to :bcase
end
我在我的控制台中关注了:
irb(main):002:0> b = a.build_bcase
=> #<Bcase id: nil, pimp_id: nil, comment_text: nil, created_at: nil, updated_at
: nil, status: nil>
irb(main):003:0> (1..10).each {|i| b.c_entries.build(:order_no => i)}
=> 1..10
irb(main):004:0> b.c_entries
=> #<ActiveRecord::Associations::CollectionProxy [#<CEntry id: nil, bcase_id: ni
l, order_no: 1, description: nil, hours: nil, nrc: nil, created_at: nil, updated
_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 2, description: nil, hours
: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id:
nil, order_no: 3, description: nil, hours: nil, nrc: nil, created_at: nil, updat
ed_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 4, description: nil, hou
rs: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id
: nil, order_no: 5, description: nil, hours: nil, nrc: nil, created_at: nil, upd
ated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 6, description: nil, h
ours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_
id: nil, order_no: 7, description: nil, hours: nil, nrc: nil, created_at: nil, u
pdated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 8, description: nil,
hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcas
e_id: nil, order_no: 9, description: nil, hours: nil, nrc: nil, created_at: nil,
updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 10, description: n
il, hours: nil, nrc: nil, created_at: nil, updated_at: nil>]>
所以,我创建了一个名为&#34; b&#34;的bcase类型的对象。然后构建10个c_entry类型的对象和:order_no从1到10. b.c_entries
向我显示它工作正常。
现在我试图通过find_by_order_no(1)
或where(:order_no => 1)
从这个数组中获取一个对象,这就是我得到的。
irb(main):018:0> b.c_entries.where(:order_no => 1)
=> #<ActiveRecord::Relation []>
irb(main):025:0> b.c_entries.find_by_order_no(1)
=> nil
那并没有给我我想要的东西,我无法找出我做错了什么。我也尝试使用.first
,但这没有帮助。
irb(main):026:0> b.c_entries.find_by_order_no(1).first
NoMethodError: undefined method `first' for nil:NilClass
from (irb):26
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.0
/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.0
/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.0
/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):027:0> b.c_entries.where(:order_no => 1).first
=> nil
irb(main):028:0> b.c_entries.first
=> #<CEntry id: nil, bcase_id: nil, order_no: 1, description: nil, hours: nil, n
rc: nil, created_at: nil, updated_at: nil>
irb(main):029:0> b.c_entries.first.order_no
=> 1
任何人都知道该怎么做?
祝你好运!
答案 0 :(得分:1)
您没有将其中的任何内容保存到数据库中。构建,只在内存中创建东西。在构建模型上调用save
或使用create
答案 1 :(得分:1)
当你说
时b = a.build_bcase
它只相当于
Bcase.new
请检查:http://apidock.com/rails/ActiveRecord/Relation/build。
您应该做的是通过致电b.save
或尝试create
来保存记录