# Creating a parent with grandparent\_id from grandchild model

**URL:** <https://www.ruby-forum.com/t/creating-a-parent-with-grandparent-id-from-grandchild-model/164717>\
**Category:** Rails\
**Created:** [April 4, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/creating-a-parent-with-grandparent-id-from-grandchild-model/164717 "2009-04-04T18:10:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhubarbcrumble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rhubarbcrumble](https://www.ruby-forum.com/u/rhubarbcrumble)\
**Post date:** [April 4, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/creating-a-parent-with-grandparent-id-from-grandchild-model/164717/1 "2009-04-04T18:10:29Z")

</div>

Hi,

I have a situation where I have mapping table to link two models  
(effectively linking a grandparent and grandchild with a child and  
parent respectively)

Here are the models:

class Town \< ActiveRecord::Base  
has\_many :venues  
end

class Venue \< ActiveRecord::Base  
has\_many :venue\_aliases  
belongs\_to :town  
validates\_presence\_of :town  
end

class VenueAlias \< ActiveRecord::Base  
belongs\_to :venue  
end

The list of towns already exists and is ultimately fixed, and for each  
new venue\_alias a venue must be assigned or created (with a town\_id) and  
assigned.

I would like to do this all in one form, preferably with a nice  
autocomplete so that the user can either:

1. Enter the venue name and where it already exists assign the venue\_id  
to the venue alias

or

1. Enter the venue name and a town name, so that the new venue/town\_id  
combo is created and subsequently the new venue\_id is assigned to the  
venue\_alias

I’ve been through Ryan B.’ screencasts but they generally deal with  
creating children at the same time as parents, so is there a way of  
doing it the other way around as it were?

Thanks in advance…
