Reputation: 393
I got two models
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
And I did following in my console:
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>]>
So, I created an object of the bcase-type with the name "b" and then build 10 objects of the c_entry-type and the :order_no from 1 to 10. And b.c_entries
shows me that it worked fine.
Now I was trying to get a single object out of this array with find_by_order_no(1)
or where(:order_no => 1)
and thats what I get.
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
And that didnt give me what I wanted and I couldnt find out what I did wrong. I also tried to use .first
but that didnt help.
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
Anyone knows what to do?
Best regards!
Upvotes: 0
Views: 63
Reputation: 183
When you are saying
b = a.build_bcase
it is just equivalent to
Bcase.new
Check this: http://apidock.com/rails/ActiveRecord/Relation/build.
What you should be doing is save the record by calling b.save
or try create
http://apidock.com/rails/ActiveRecord/Base/create/class
Upvotes: 1
Reputation: 8044
You did not save any of them into the database. Build, only creates stuff in memory. Either call save
on build models or use create
Upvotes: 1