Government Security
Network Security Resources

Jump to content

Photo

Ruby On Rails - Creating Multiple Entities Within A Single Entity's Method


  • Please log in to reply
1 reply to this topic

#1 DefAce

DefAce

    Private

  • Members
  • 8 posts

Posted 15 June 2010 - 04:11 PM

I have a model for a piece of software (it has the name, key, publisher, and other info) and one of the fields is for an invoice id.
I want to create a new invoice in the database first, get its id, give it to the software entry to store, have that entry return its id, and assign that id to the invoice entry that was just created. So far I've just edited the new.html.erb file to add extra params to be passed as :invoice and not :software to the invoice model when its created. Here's my softwares_controller's create (or at least the relevant part):

def create
@invoice = Invoice.new(params[:invoice])
@invoice.save
params[:software][:invoice_id] = @invoice.id
@software = Software.new(params[:software])
.
.
.
end

But when I try this:

@inv = Invoice.find(id == @software.invoice_id).Software_id = @software.id #this might not work -- could only be editing the instance variable's software id
@inv.save

I always get back that @software.invoice_id = nil, false, or 0.

Is there a better approach to doing something like this?

#2 DefAce

DefAce

    Private

  • Members
  • 8 posts

Posted 17 June 2010 - 04:02 PM

I have a model for a piece of software (it has the name, key, publisher, and other info) and one of the fields is for an invoice id.
I want to create a new invoice in the database first, get its id, give it to the software entry to store, have that entry return its id, and assign that id to the invoice entry that was just created. So far I've just edited the new.html.erb file to add extra params to be passed as :invoice and not :software to the invoice model when its created. Here's my softwares_controller's create (or at least the relevant part):

def create
@invoice = Invoice.new(params[:invoice])
@invoice.save
params[:software][:invoice_id] = @invoice.id
@software = Software.new(params[:software])
.
.
.
end

But when I try this:

@inv = Invoice.find(id == @software.invoice_id).Software_id = @software.id #this might not work -- could only be editing the instance variable's software id
@inv.save

I always get back that @software.invoice_id = nil, false, or 0.

Is there a better approach to doing something like this?


Well for those of you wondering, I didn't find a more rails-automated approach to this - no commands or anything that rails will automatically handle behind the scenes - probably because I'm a RoR nub but, I was able to do what I wanted and I'll share that with you in case anyone out there is wondering.

My updated code is:

def create
@invoice = Invoice.new(params[:invoice])
@invoice.save
params[:software][:invoice_id] = @invoice.id
@software = Software.new(params[:software])

respond_to do |format|
if @software.save
Invoice.update(@invoice.id, :software_id => @software.id)
.
.
.
end


so it's Model.update(:id, :field => new_value, :field => new_value . . .) that saved the day. Hope this helps: if you gotta make multiple entities through a single entity's method then try this approach, unless there's a more rails way of doing this - which is what I'm still hoping someone will post up here. :P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users