Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Created November 15, 2015 02:31
Show Gist options
  • Save cat-in-136/b4494cce06a06f438559 to your computer and use it in GitHub Desktop.
Save cat-in-136/b4494cce06a06f438559 to your computer and use it in GitHub Desktop.
#ActiveRecord, count may not equal to length
class Child < ActiveRecord::Base
belongs_to :parent
end
class Parent < ActiveRecord::Base
has_many :child, :dependent => :destroy
accepts_nested_attributes_for :child, :allow_destroy => true
end
parent = Parent.new
parent.child.build
parent.child
# => #<ActiveRecord::Associations::CollectionProxy [#<Child id: nil, parent_id: nil, created_at: nil, updated_at: nil>]>
parent.child.length
# => 1
parent.child.count
# => 0
parent.child.size
# => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment