Forum: Ruby on Rails Creating a parent with grandparent_id from grandchild model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sandy T. (Guest)
on 2009-04-04 20:10
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

2. 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...
This topic is locked and can not be replied to.