Hi Mark
there is an article here
http://wiki.rubyonrails.org/rails/pages/Beginner+Howto+on+has_many+:through
but I don't like it, since it has its proprietary link table, and I assume you want a default link table
I gave it some thoughts, and here is how I would get started
show model
---------------
class Show < ActiveRecord::Base
has_and_belongs_to_many:artists
end
artist model
----------------
class Artist < ActiveRecord::Base
has_and_belongs_to_many:shows
end
controller for artist
----------------------
...
def new
@artist = Artist.new
@shows = Show.find_all
end
...
new.rhtml
------------
New artist
<% form_tag :action => 'create' do %>
<%= render :partial => 'form' %>
<%= submit_tag "Create" %>
<% end %>
all you need to do then is make sure that the appears_in= method exists in the model
so you need to extend the artist model
class Artist < ActiveRecord::Base
has_and_belongs_to_many:shows
def appears_in= showid
shows << Show.find(showid)
end
end
this will definitely work,
but I am afraid that the link will be stored in the link table
prior to the actual storage of the new artist
(which in the end could lead to erroneous links)
I have to think a bit about this,
but it gives you something to get started
maybe you need to work with stub methods in the artist model
that only get written on-save
I definitely think that writeable methods in the model are the safest solution
... certainly a lot less annoying than the method in the referenced article
weird, I never really came accross this, and there is hardly any literature about it :-(
cheers
Geert