# Remember ids in forms (with association)

**URL:** <https://www.ruby-forum.com/t/remember-ids-in-forms-with-association/172893>\
**Category:** Rails\
**Created:** [July 29, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/remember-ids-in-forms-with-association/172893 "2009-07-29T14:51:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![plplpl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@plplpl](https://www.ruby-forum.com/u/plplpl)\
**Post date:** [July 29, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/remember-ids-in-forms-with-association/172893/1 "2009-07-29T14:51:38Z")

</div>

Hi there,

I’m building something and I think I’m not going the Rails way with  
something. I have two models: ‘seasons’ and ‘episodes’.

# Season model:

has\_many :episodes, :dependent =\> :destroy

# Episode model:

belongs\_to :season

# routes.rb

map.resources :seasons, :has\_many =\> :episodes  
map.resources :episodes

Episodes belong to seasons, so when I want to create a new episode, I  
use this path: new\_season\_episode\_path(season). I create a new episode  
instance like this:

@season = Season.find(params[:season\_id])  
@episode = @season.episodes.new

Everything’s going fine, untill I want to save the whole thing. The  
form redirects to ‘/episodes’ (no ID). But I have to save the  
season\_id in the episode.

@show = Show.find( season id? )  
@episode = @show.episodes.create(params[:episode])

Anyone suggestions on how to save it and not use hidden fields in the  
form (or do I have to?)?  
I hope this is clear enough.

pl

---

<div class="post-metadata">

**Author:** ![plplpl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@plplpl](https://www.ruby-forum.com/u/plplpl)\
**Post date:** [July 29, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/remember-ids-in-forms-with-association/172893/2 "2009-07-29T15:02:11Z")

</div>

Someday, I hope to know Rails well enough that answers to questions  
like this just roll off the tip of my tongue (or, in this case, the  
tips of my fingers as I type on the keyboard). For now, the best  
answer I can give you is to reference the web page where I learned how  
to use nested routes:

> **[Rolling with Rails 2.0 - The First Full Tutorial - Part 1 | AkitaOnRails.com](https://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial)**
>
> for brazilians: click here.
> I am very happy to see that my Rails 2.0 Screencast was very well received. More than 1,500 unique visitors watched it...

I suspect that you want to use @season.episodes.build instead of  
@season.episodes.new, that everything might save correctly when you  
save @season, and that you should not be redirecting to /episodes.  
But I am nowhere close to being an expert, so I could be sending you  
off in the wrong direction.

–wpd
