Specify table names for the join table

On page 227 of AWDR pdf version, “Rails will automatically find the join
table categories_products linking categories and products. If you used
some
other name, you’ll need to add a declaration so Rails can find it.”

What’s the syntax to declare that? set_table_name?

Thanks,

Sky

Hi,

I’m trying to dynamically add items to a select list with Ajax. I’m
displaying the options of the list with render_partial_collection and
passing it a list of galleries. When the page is hit initially the
drop-down list renders correctly (with galleries from my db). When I
submit my form (using form_remote_tag) with the new gallery to add, my
controller takes a turn at rendering the same partial, but spits out
plain text within the select tags instead of option elements. Right, so
here’s the code…

controller: test_controller.rb
class TestController < ApplicationController
def index
@galleries = Gallery.find(:all).map { |g| g.name }
end
def add
@newGal = Gallery.new
@newGal.name = @params[‘new_gallery’]
@newGal.save
@galleries = Gallery.find(:all).map { |g| g.name }
render_partial_collection ‘shared/gallery’, @galleries
end
end

_partial: shared/gallery.rhtml

<%= gallery %>

view: index.rhtml
<%= form_remote_tag(:update => “update_galleries”,
:complete => evaluate_remote_response,
:url => { :action => :add } ) %>
<%= text_field_tag :new_gallery %>
<%= submit_tag “Submit” %>
<%= end_form_tag %>

<%= render_partial_collection 'shared/gallery', @galleries %>

What’s interesting to me is that if I use a div tag instead of a
select, and add, say, a list of links using my partial, the html is
retained. Perhaps I’ve just been staring at this too long this morning.
Ideas?

Thanks,
Rob

Hi,

2005/11/26, Sky Y. [email protected]:

On page 227 of AWDR pdf version, “Rails will automatically find the join
table categories_products linking categories and products. If you used some
other name, you’ll need to add a declaration so Rails can find it.”

What’s the syntax to declare that? set_table_name?

It’s in the model where you declare has_and_belongs_to_many.

Have a look here:
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000467

HTH,
Beate