When Empty Isn’t Empty With Rails Active Record Collections
So I'm writing some Rails code, just playing around, and I run into a very strange situation. I've found a situation where Rails' Active Record code is calculating the results to empty? incorrectly, even if I add elements to the collection . So I had a has_many collection with elements inside of it, but calls to length or empty were returning 0 and true . Here's my simple models: class Query < ActiveRecord::Base has_many :measures validates_presence_of :cube_name def measure_attributes=(attrs) attrs.each {|a| measures.build(a)} end end and class Measure :query has_one :condition, :as => :conditionable end Notice that in Query, I've added a measure_attributes method which automatically handles the construction of new Measure instances from the form parameters. Here's the simple nested form. From the models, a Query has many Measures: <% form_for :query, :url => {:action => :new} do |f| %> Cube Name: <%= f.text_field :...