Walksalong
2006-Dec-21 15:26 UTC
Newbie Question: multiple models - need ID from one table to populate the other
Hello,
I am creating a new record in my Elements table and that is pretty
straight forward. But I need to add an ID to the Elements table that
comes from another model - Pages. How can I create a record in the
Pages table and then use its ID to populate the Elements table?
I was trying something like this in the ElementsController:
def create
@page = Page.new(params[:page])
@element = Element.new(params[:element])
if @element.save
flash[:notice] = ''{@element.type.capitalize} was successfully
created.''
flash[:inform] = true;
render :partial => show
end
end
But no luck so far - any ideas?
Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Matt Beedle
2006-Dec-21 16:34 UTC
Re: Newbie Question: multiple models - need ID from one tabl
I''m not sure I''m understanding this properly , but I think
this should
work:
@page = Page.new( params[:page] )
if @page.save
@element = Element.new( params[ :element ] )
@element.page_id = @page.id
if @element.save
# do something
else
# element did not save, do something else
else
# Page did not save
end
--
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
-~----------~----~----~----~------~----~------~--~---
Bill Walton
2006-Dec-21 16:37 UTC
Re: Newbie Question: multiple models - need ID from one table to populate the other
Walksalong wrote:> I need to add an ID to the Elements table that > comes from another model - Pages. How can > I create a record in the Pages table and then use > its ID to populate the Elements table?> def create > @page = Page.new(params[:page])@page.save # it won''t have an id until it''s saved> @element = Element.new(params[:element])@element.page_id = @page.id> if @element.save > flash[:notice] = ''{@element.type.capitalize} was successfully > created.'' > flash[:inform] = true; > render :partial => show > end > endhth, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matt Beedle
2006-Dec-21 16:41 UTC
Re: Newbie Question: multiple models - need ID from one tabl
ok, so how about this?
@page = Page.new( params[:page] )
if @page.save
@element = Element.new( params[ :element ] )
@page = Page.find( :last, @page.title )
@element.page_id = @page.id
if @element.save
# do something
else
# element did not save, do something else
else
# Page did not save
end
--
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
-~----------~----~----~----~------~----~------~--~---
Matt Beedle
2006-Dec-21 16:42 UTC
Re: Newbie Question: multiple models - need ID from one tabl
Sorry, I meant:
@page = Page.new( params[:page] )
if @page.save
@element = Element.new( params[ :element ] )
@page = Page.find( :last, :conditions => "title = #{@page.title}"
)
@element.page_id = @page.id
if @element.save
# do something
else
# element did not save, do something else
else
# Page did not save
end
--
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
-~----------~----~----~----~------~----~------~--~---
Matt Beedle
2006-Dec-21 17:00 UTC
Re: Newbie Question: multiple models - need ID from one tabl
Also, if the page title is not unique, you could try adding a created_on field to the page table and then selecting only pages which have been created in the last minute with the same title. --- <a target="_blank" href="http://www.matt-beedle.com">matt-beedle.com</a> <a target="_blank" href="htttp://www.best-mobile-phones.org">best-mobile-phones.org</a> -- 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 -~----------~----~----~----~------~----~------~--~---
Walksalong
2006-Dec-21 18:35 UTC
Re: Newbie Question: multiple models - need ID from one tabl
Thanks Matt! I''ll try that now... On Dec 21, 12:00 pm, Matt Beedle <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Also, if the page title is not unique, you could try adding a created_on > field to the page table and then selecting only pages which have been > created in the last minute with the same title. > > --- > > <a target="_blank" href="http://www.matt-beedle.com">matt-beedle.com</a> > > <a target="_blank" > href="htttp://www.best-mobile-phones.org">best-mobile-phones.org</a> > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Is this line really needed?
@page = Page.find( :last, :conditions => "title = #{@page.title}"
)
@page already exists and should respond to @page.id and you will save a
search on the database.
--ABS
Matt Beedle wrote:> Sorry, I meant:
>
> @page = Page.new( params[:page] )
> if @page.save
> @element = Element.new( params[ :element ] )
> @page = Page.find( :last, :conditions => "title =
#{@page.title}" )
> @element.page_id = @page.id
> if @element.save
> # do something
> else
> # element did not save, do something else
> else
> # Page did not save
> end
>
> --
> 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
-~----------~----~----~----~------~----~------~--~---
Walksalong
2006-Dec-21 19:56 UTC
Re: Newbie Question: multiple models - need ID from one tabl
Both work -- Thanks! On Dec 21, 2:52 pm, "askegg" <andrew.sk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is this line really needed? > @page = Page.find( :last, :conditions => "title = #...@page.title}" ) > > @page already exists and should respond to @page.id and you will save a > search on the database. > > --ABS > > Matt Beedle wrote: > > Sorry, I meant: > > > @page = Page.new( params[:page] ) > > if @page.save > > @element = Element.new( params[ :element ] ) > > @page = Page.find( :last, :conditions => "title = #...@page.title}" ) > > @element.page_id = @page.id > > if @element.save > > # do something > > else > > # element did not save, do something else > > else > > # Page did not save > > end > > > -- > > 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 -~----------~----~----~----~------~----~------~--~---