has_many and the << operator ?

In the AWDWR book it mentions that the << operator (or method?) just adds the child model the child collection within the parent model.

For some reason when I am using it it doesn't work as expected.

Here is what I have: class Member    has_many :tasks end

class Task    belongs_to :member end

describe Member, " linkage to task" do   before(:each) do     @member = Member.new(valid_member_params)   end

  def valid_member_params     {      :first_name => "Joe",      ...     }   end

  it "should be added through a member" do     task = Task.new(:content => "Do something")     @member.tasks << task     @member.save

    @ member.should have(1).tasks     @ member.tasks.size == 1     @ member.tasks[0].id > 0 #FAILS here as it never got inserted

  end end

The log is rather odd as it doesn't show any hits to the database: Spec::Rails::DSL::HelperEvalContextController: missing default helper path spec/rails/dsl/helper_eval_context_helper Spec::Rails::DSL::ViewExampleController: missing default helper path spec/rails/dsl/view_example_helper   e[4;36;1mSQL (0.000187)e[0m e[0;1mSET SQL_AUTO_IS_NULL=0e[0m   e[4;35;1mSQL (0.000142)e[0m e[0mBEGINe[0m   e[4;36;1mMembers Columns (0.006676)e[0m e[0;1mSHOW FIELDS FROM memberse[0m   e[4;35;1mTask Columns (0.007478)e[0m e[0mSHOW FIELDS FROM taskse[0m   e[4;36;1mSQL (0.000180)e[0m e[0;1mROLLBACKe[0m   e[4;35;1mSQL (0.000134)e[0m e[0mBEGINe[0m   e[4;36;1mSQL (0.000133)e[0m e[0;1mROLLBACKe[0m

Thanks for the help

(and I have no idea why I am getting the warning of missing the default helper path)

i think you're hitting the limitation that you can't #<< to a parent that's not been saved

http://alloycode.com/2007/10/11/when-things-get-saved http://dev.rubyonrails.org/changeset/7511

That is the way that I had it at first, but after referring back to the book (pg 330 at the bottom) it shows the save done after the child model is added to the parent.

I have tried again doing what you said and it is still failing: ... @listing.save @listing.online_showings << showing @listing.online_showings[0].id > 0

As a rule of thumb, I would be calling save!, as long as I wasn't checking for errors. Maybe save is failing?

///ark

Thanks for the help.

I moved the save to after all the child models were added and for some reason started getting a validation error. I then clued in that the validates_as_attachment was failing for the attachment_fu plugin. Once I disabled that it started working as expected.

Thanks again.

I am still using 1.2.5, although that would explain the NoMethodError, which somehow disappeared.