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
[4;36;1mSQL (0.000187) [0m [0;1mSET SQL_AUTO_IS_NULL=0 [0m
[4;35;1mSQL (0.000142) [0m [0mBEGIN [0m
[4;36;1mMembers Columns (0.006676) [0m [0;1mSHOW FIELDS FROM
members [0m
[4;35;1mTask Columns (0.007478) [0m [0mSHOW FIELDS FROM
tasks [0m
[4;36;1mSQL (0.000180) [0m [0;1mROLLBACK [0m
[4;35;1mSQL (0.000134) [0m [0mBEGIN [0m
[4;36;1mSQL (0.000133) [0m [0;1mROLLBACK [0m
Thanks for the help
(and I have no idea why I am getting the warning of missing the
default helper path)
On Nov 2, 7:38 pm, chris [email protected] wrote:
class Task
:first_name => “Joe”,
@ member.tasks.size == 1
[4;36;1mSQL (0.000187) [0m [0;1mSET SQL_AUTO_IS_NULL=0 [0m
(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
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.
As a rule of thumb, I would be calling save!, as long as I wasn’t
checking for errors. Maybe save is failing?
///ark
I am still using 1.2.5, although that would explain the NoMethodError,
which somehow disappeared.
On Nov 3, 9:28 am, Josh S. [email protected]
Chris O. wrote:
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.
What version of Rails are you using? There was a bug in edge (
http://dev.rubyonrails.org/ticket/9989 ) that hit has_many << that got
fixed very recently. It prevented << from working on has_many assocs for
new objects where the assoc hasn’t been mentioned yet. That was fixed
last week in [8049].
–
Josh S.
http://blog.hasmanythrough.com