Hi, I am having a problem saving a new model that has an associated child model. If a new ticket is being created and will have one comment to go with it then I tried this in my action @ticket = Ticket.new(params[:ticket]) @comment = Comment.new(params[:comment]) @ticket.comments << @comment if @ticket.save @notice = "Ticket added. Thanks for contributing!" @ticket = Ticket.new @comment = Comment.new else @notice = "Sorry, unable to add ticket." end The save always fails because the comment does not have a ticket_id specified. How can I specify the ticket_id for the comment if the ticket id doesn''t even exist before I call save. I thought Rails would take care of this for me when I use the << or push(). (Rails 1.0) Any ideas what to do? Thanks, Peter
On 4/25/06, Peter Michaux <petermichaux@gmail.com> wrote:> Hi, > > I am having a problem saving a new model that has an associated child > model. If a new ticket is being created and will have one comment to > go with it then I tried this in my action[snip] I am current going to all this effort which just seems wrong. @ticket = Ticket.new(params[:ticket]) @comment = Comment.new(params[:comment]) if @ticket.save @comment.ticket_id = @ticket.id @ticket.comments << @comment if @comment.save @notice = "Ticket added" else @ticket.destroy @notice = "Sorry, unable to add ticket." end else @notice = "Sorry, unable to add ticket." end There must be a simpler way. Any ideas? Thanks, Peter
You almost have it. Try: def new @ticket = Ticket.new @comment = Comment.new render :action => ''new'' end def create @ticket = Ticket.new(params[:ticket]) @comment = Comment.new(params[:comment]) @comment.ticket = @ticket if @comment.save flash[:notice] = "Ticket added. Thanks for contributing!" redirect_to :action => ''new'' else flash[:notice] = "Sorry, unable to add ticket." render :action => ''new'' end end -- Posted with http://DevLists.com. Sign up and save your mailbox.
Hi Jeremy, Thanks for the code. Unfortunately it didn''t work:S The save still results in an error because the ticket_id is not set for the comment. What to do? Thanks Peter On 27 Apr 2006 14:14:52 -0000, Jeremy Maziarz <devlists-rubyonrails@devlists.com> wrote:> You almost have it. Try: > > def new > @ticket = Ticket.new > @comment = Comment.new > render :action => ''new'' > end > > def create > @ticket = Ticket.new(params[:ticket]) > @comment = Comment.new(params[:comment]) > @comment.ticket = @ticket > > if @comment.save > flash[:notice] = "Ticket added. Thanks for contributing!" > redirect_to :action => ''new'' > else > flash[:notice] = "Sorry, unable to add ticket." > render :action => ''new'' > end > end > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi, Please tell me there is something simpler. Peter On 4/26/06, Peter Michaux <petermichaux@gmail.com> wrote:> On 4/25/06, Peter Michaux <petermichaux@gmail.com> wrote: > > Hi, > > > > I am having a problem saving a new model that has an associated child > > model. If a new ticket is being created and will have one comment to > > go with it then I tried this in my action > > [snip] > > I am current going to all this effort which just seems wrong. > > @ticket = Ticket.new(params[:ticket]) > @comment = Comment.new(params[:comment]) > > if @ticket.save > @comment.ticket_id = @ticket.id > @ticket.comments << @comment > if @comment.save > @notice = "Ticket added" > else > @ticket.destroy > @notice = "Sorry, unable to add ticket." > end > else > @notice = "Sorry, unable to add ticket." > end > > There must be a simpler way. Any ideas? > > Thanks, > Peter >
Is the comment attached with a through relationship, or a straight has_many? the << does not work with through relations. Otherwise with a normal has_many, @ticket = Ticket.new(params[:ticket]) @ticket.comments << Comment.new(params[:comment]) @ticket.save! should set the foreign keys automatically in the Comment object as I understand it. Could you post the relevant lines of your Ticket model? On 5/1/06, Peter Michaux <petermichaux@gmail.com> wrote:> > Hi, > > Please tell me there is something simpler. > > Peter > > On 4/26/06, Peter Michaux <petermichaux@gmail.com> wrote: > > On 4/25/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > Hi, > > > > > > I am having a problem saving a new model that has an associated child > > > model. If a new ticket is being created and will have one comment to > > > go with it then I tried this in my action > > > > [snip] > > > > I am current going to all this effort which just seems wrong. > > > > @ticket = Ticket.new(params[:ticket]) > > @comment = Comment.new(params[:comment]) > > > > if @ticket.save > > @comment.ticket_id = @ticket.id > > @ticket.comments << @comment > > if @comment.save > > @notice = "Ticket added" > > else > > @ticket.destroy > > @notice = "Sorry, unable to add ticket." > > end > > else > > @notice = "Sorry, unable to add ticket." > > end > > > > There must be a simpler way. Any ideas? > > > > Thanks, > > Peter > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060501/5d474b91/attachment-0001.html
Hi Liquid, On 4/30/06, Liquid <has.sox@gmail.com> wrote:> Is the comment attached with a through relationship, or a straight has_many?the has_many is just a regular relationship> Otherwise with a normal has_many, > > > @ticket = Ticket.new(params[:ticket]) > @ticket.comments << Comment.new(params[:comment]) > @ticket.save! > > should set the foreign keys automatically in the Comment object as I > understand it. > > Could you post the relevant lines of your Ticket model?class Ticket < ActiveRecord::Base has_many :comments, :dependent=>:delete_all end Any ideas why it might not work? I doubt this is a bug in Rails but I don''t seem to be doing anything obviously wrong. Thanks, Peter
I''m at work at the moment, I''ll look at it when I get home... Just one more thought though. Have you tried @ticket.comments.create( Comment.new(params[:comment]) This may work... Failing this I will try it when I get home On 5/1/06, Peter Michaux <petermichaux@gmail.com> wrote:> > Hi Liquid, > > On 4/30/06, Liquid <has.sox@gmail.com> wrote: > > Is the comment attached with a through relationship, or a straight > has_many? > > the has_many is just a regular relationship > > > Otherwise with a normal has_many, > > > > > > @ticket = Ticket.new(params[:ticket]) > > @ticket.comments << Comment.new(params[:comment]) > > @ticket.save! > > > > should set the foreign keys automatically in the Comment object as I > > understand it. > > > > Could you post the relevant lines of your Ticket model? > > class Ticket < ActiveRecord::Base > has_many :comments, :dependent=>:delete_all > end > > Any ideas why it might not work? I doubt this is a bug in Rails but I > don''t seem to be doing anything obviously wrong. > > Thanks, > Peter > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060501/77061ace/attachment.html
Hi Peter, I had tried some test code without any problems. I have included the code below for the example that I used. Can you post your table definitions? Table Defs: tickets id INT title STRING comments id INT description STRING ticket_id INT Ticket Model: <code lang=''ruby''> class Ticket < ActiveRecord::Base has_many :comments, :dependent => :delete_all end </code> Comment Model: <code lang=''ruby''> class Comment < ActiveRecord::Base belongs_to :ticket end </code> Ticket Controller: <code lang=''ruby''> class TicketController < ApplicationController def list @tickets = Ticket.find(:all) end def new if request.post? @ticket = Ticket.new(params[:ticket]) @ticket.comments << Comment.new(params[:comment]) if @ticket.save flash[:notice] = "Saved" redirect_to :action => :list else flash[:notice] = "Not Saved" end else @ticket = Ticket.new end end end </code> Ticket View - New <code lang=''ruby''> <%= start_form_tag :action => :new %> Ticket Title: <%= text_field "ticket", "title" %> <br/> Comment: <%= text_field "comment", "description" %> <br/> <%= submit_tag %> <%= end_form_tag %> </code> On 5/1/06, Liquid <has.sox@gmail.com> wrote:> > I''m at work at the moment, I''ll look at it when I get home... > > Just one more thought though. > > Have you tried > > @ticket.comments.create( Comment.new(params[:comment]) > > This may work... Failing this I will try it when I get home > > > On 5/1/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > Hi Liquid, > > > > On 4/30/06, Liquid <has.sox@gmail.com> wrote: > > > Is the comment attached with a through relationship, or a straight > > has_many? > > > > the has_many is just a regular relationship > > > > > Otherwise with a normal has_many, > > > > > > > > > @ticket = Ticket.new(params[:ticket]) > > > @ticket.comments << Comment.new(params[:comment]) > > > @ticket.save! > > > > > > should set the foreign keys automatically in the Comment object as I > > > understand it. > > > > > > Could you post the relevant lines of your Ticket model? > > > > class Ticket < ActiveRecord::Base > > has_many :comments, :dependent=>:delete_all > > end > > > > Any ideas why it might not work? I doubt this is a bug in Rails but I > > don''t seem to be doing anything obviously wrong. > > > > Thanks, > > Peter > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060501/cd980576/attachment.html
Hi Liquid, Thank you for the investigation. Wow! In my table definitions I have something like id INT NOT NULL I will try changing this and let you know what happens. It seems like this is probably the problem. I really appreciate your help. Peter On 5/1/06, Liquid <has.sox@gmail.com> wrote:> Hi Peter, > > I had tried some test code without any problems. I have included the code > below for the example that I used. > > Can you post your table definitions? > > Table Defs: > tickets > id INT > title STRING > > comments > id INT > description STRING > ticket_id INT > > > Ticket Model: > <code lang=''ruby''> > > > class Ticket < ActiveRecord::Base > has_many :comments, :dependent => :delete_all > end > > </code> > > Comment Model: > <code lang=''ruby''> > > class Comment < ActiveRecord::Base > belongs_to :ticket > end > > </code> > > Ticket Controller: > <code lang=''ruby''> > > class TicketController < ApplicationController > > def list > @tickets = Ticket.find(:all) > end > > def new > if request.post? > > @ticket = Ticket.new(params[:ticket]) > @ticket.comments << Comment.new(params[:comment]) > if @ticket.save > flash[:notice] = "Saved" > redirect_to :action => :list > else > flash[:notice] = "Not Saved" > end > else > @ticket = Ticket.new > end > end > > end > > </code> > > Ticket View - New > <code lang=''ruby''> > > <%= start_form_tag :action => :new %> > Ticket Title: <%= text_field "ticket", "title" %> > <br/> > Comment: <%= text_field "comment", "description" %> > <br/> > <%= submit_tag %> > <%= end_form_tag %> > > </code> > > > > > > > On 5/1/06, Liquid <has.sox@gmail.com> wrote: > > > > I''m at work at the moment, I''ll look at it when I get home... > > > > Just one more thought though. > > > > Have you tried > > > > @ticket.comments.create( Comment.new(params[:comment]) > > > > This may work... Failing this I will try it when I get home > > > > > > > > > > On 5/1/06, Peter Michaux < petermichaux@gmail.com> wrote: > > > > > Hi Liquid, > > > > > > On 4/30/06, Liquid <has.sox@gmail.com> wrote: > > > > Is the comment attached with a through relationship, or a straight > has_many? > > > > > > the has_many is just a regular relationship > > > > > > > Otherwise with a normal has_many, > > > > > > > > > > > > @ticket = Ticket.new(params[:ticket]) > > > > @ticket.comments << Comment.new(params[:comment]) > > > > @ticket.save! > > > > > > > > should set the foreign keys automatically in the Comment object as I > > > > understand it. > > > > > > > > Could you post the relevant lines of your Ticket model? > > > > > > class Ticket < ActiveRecord::Base > > > has_many :comments, :dependent=>:delete_all > > > end > > > > > > Any ideas why it might not work? I doubt this is a bug in Rails but I > > > don''t seem to be doing anything obviously wrong. > > > > > > Thanks, > > > Peter > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
I don''t think that will help you. The table defs that I used I did not completely specify in the post. The ID column is NOT NULL that I used in both tables. All I included in the post was the column name and type. Sorri for the inconvienience. On 5/2/06, Peter Michaux <petermichaux@gmail.com> wrote:> > Hi Liquid, > > Thank you for the investigation. Wow! > > In my table definitions I have something like > > id INT NOT NULL > > I will try changing this and let you know what happens. It seems like > this is probably the problem. > > I really appreciate your help. > > Peter > > On 5/1/06, Liquid <has.sox@gmail.com> wrote: > > Hi Peter, > > > > I had tried some test code without any problems. I have included the > code > > below for the example that I used. > > > > Can you post your table definitions? > > > > Table Defs: > > tickets > > id INT > > title STRING > > > > comments > > id INT > > description STRING > > ticket_id INT > > > > > > Ticket Model: > > <code lang=''ruby''> > > > > > > class Ticket < ActiveRecord::Base > > has_many :comments, :dependent => :delete_all > > end > > > > </code> > > > > Comment Model: > > <code lang=''ruby''> > > > > class Comment < ActiveRecord::Base > > belongs_to :ticket > > end > > > > </code> > > > > Ticket Controller: > > <code lang=''ruby''> > > > > class TicketController < ApplicationController > > > > def list > > @tickets = Ticket.find(:all) > > end > > > > def new > > if request.post? > > > > @ticket = Ticket.new(params[:ticket]) > > @ticket.comments << Comment.new(params[:comment]) > > if @ticket.save > > flash[:notice] = "Saved" > > redirect_to :action => :list > > else > > flash[:notice] = "Not Saved" > > end > > else > > @ticket = Ticket.new > > end > > end > > > > end > > > > </code> > > > > Ticket View - New > > <code lang=''ruby''> > > > > <%= start_form_tag :action => :new %> > > Ticket Title: <%= text_field "ticket", "title" %> > > <br/> > > Comment: <%= text_field "comment", "description" %> > > <br/> > > <%= submit_tag %> > > <%= end_form_tag %> > > > > </code> > > > > > > > > > > > > > > On 5/1/06, Liquid <has.sox@gmail.com> wrote: > > > > > > I''m at work at the moment, I''ll look at it when I get home... > > > > > > Just one more thought though. > > > > > > Have you tried > > > > > > @ticket.comments.create( Comment.new(params[:comment]) > > > > > > This may work... Failing this I will try it when I get home > > > > > > > > > > > > > > > On 5/1/06, Peter Michaux < petermichaux@gmail.com> wrote: > > > > > > > Hi Liquid, > > > > > > > > On 4/30/06, Liquid <has.sox@gmail.com> wrote: > > > > > Is the comment attached with a through relationship, or a straight > > has_many? > > > > > > > > the has_many is just a regular relationship > > > > > > > > > Otherwise with a normal has_many, > > > > > > > > > > > > > > > @ticket = Ticket.new(params[:ticket]) > > > > > @ticket.comments << Comment.new(params[:comment]) > > > > > @ticket.save! > > > > > > > > > > should set the foreign keys automatically in the Comment object > as I > > > > > understand it. > > > > > > > > > > Could you post the relevant lines of your Ticket model? > > > > > > > > class Ticket < ActiveRecord::Base > > > > has_many :comments, :dependent=>:delete_all > > > > end > > > > > > > > Any ideas why it might not work? I doubt this is a bug in Rails but > I > > > > don''t seem to be doing anything obviously wrong. > > > > > > > > Thanks, > > > > Peter > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060501/53a270b1/attachment.html