Assumptions:
1) parameter holding the Venue Type is params[:type]
2) cloumn in venue_classes tabel holding the type i named "type"
3) Letter for searching the Place names is in params[:place]
4) Relations:
VenueClasses has_and_belongs_to_many :places
Places has_and_belongs_to_many :venue_classes
#prepare the Place letter for wildcard-matching in a LIKE statement
placename = params[:place] + '%'
#get the venue id for the given type
venueid = VenueClasses.find( :first, :conditions => [type =
?,params[:type]]).id
#get the venue with all the places in a collection
@venue = VenuesClasses.find :first, :conditions => ["venue_classes.id =
? AND places.name LIKE",venueid,placename], :include => :places
then you should be able to read all atrributes of venue, and all
matching places through @venue.places
Note: i'm wondering if one could also directly put the search for the
type in the second find, without first finding the ID, like this:
though i'm not so sure about it because of the special habtm stuff with
the join table
oh and yeah, my code could very possibly contain typos or other errors,
maybe it points in the right direction at least.
The ? just means to substitute in the next thing in the array.
(And _nobody_ here should be writing a Rails app without /Agile Web Development with Rails/ by the Daves. Don't let the "Agile" term spook you; they really mean "the easy way" there.)
This also works:
[ " type => :type ", params ]
Because then ActiveRecord treats the next item in the array as a Hash, and keys it with :type.
Also, the use of :include - from the API i got the impression that you
needed to use :joins first. So i guess I don't understand it yet.
If you mean a method takes two symbols, like this:
foo( :include => 42, :joins => 'bar' )
The method foo() might not use those arguments in that order. It might fetch the :joins key first.
Actually, this is my first time making a join in Rails. Is this the
right way to do it?
No, you should use the has_many directives, so they will take care of the verbose details for you.