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)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Nov 2, 7:38 pm, chris <olsen.ch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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)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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Nov 2, 8:59 pm, gene tani <gene.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 2, 7:38 pm, chris <olsen.ch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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) > > 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-savedhttp://dev.rubyonrails.org/changeset/7511--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
As a rule of thumb, I would be calling save!, as long as I wasn''t checking for errors. Maybe save is failing? ///ark> @listing.save > @listing.online_showings << showing > @listing.online_showings[0].id > 0--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Nov 3, 2:49 am, Mark Wilden <m...-OCn100epQuBBDgjK7y7TUQ@public.gmane.org> wrote:> As a rule of thumb, I would be calling save!, as long as I wasn''t > checking for errors. Maybe save is failing? > > ///ark > > > @listing.save > > @listing.online_showings << showing > > @listing.online_showings[0].id > 0--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Olsen 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I am still using 1.2.5, although that would explain the NoMethodError, which somehow disappeared. On Nov 3, 9:28 am, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 Susserhttp://blog.hasmanythrough.com > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---