talknightlife
unread,Jul 2, 2008, 5:08:52 PM7/2/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ActiveScaffold : Ruby on Rails plugin
Many thanks yet again to all of the people who are working on Active
Scaffold. I love this thing and it has made my life so much simpler.
I'm having a problem getting a nested scaffold working with a HABTM
relationship. I've got two models that are related to each other like
so:
class Event < ActiveRecord::Base
has_and_belongs_to_many :venues
...
end
class Venue < ActiveRecord::Base
has_and_belongs_to_many :events
...
end
I've got a scaffold working perfectly for Venues, and it includes a
nested link to an Events controller:
class Venues::VenuesController < ApplicationController
active_scaffold :venues do |config|
...
config.nested.add_link("Events", [:events])
end
...
end
The problem is that no matter what is in the Events controller, I get
an error message when I click on "Events" on any Venue row that looks
like this:
Unknown column 'events_venues.venue_id' in 'where clause': SELECT
DISTINCT `events`.id FROM `events` WHERE (events_venues.venue_id =
'1') ORDER BY events.`id` ASC LIMIT 0, 15
The problem here is that Events and Venues are associated through a
join table, and the join table isn't in the query. I did a bunch of
research and read a lot of Active Scaffold code, and it looks like the
active_scaffold_joins feature should allow me to manually add the join
table to that query. Is that what that's for?
I tried that by stripping down my Events controller to nothing so that
it looks like this:
class Venues::EventsController < ApplicationController
active_scaffold :events
def conditions_for_collection
@active_scaffold_joins << :events_venues
''
end
end
I do need to set up a real conditions_for_collection, but for now I'm
just trying to get something simple to work, so I've got it returning
an empty string. When I use this code and I click on the "Events"
link on a Venue row, now I get this error instead, and it never even
gets to the point of sending a SQL query:
Association named 'events_venues' was not found; perhaps you
misspelled it?
Hm. So active_scaffold_joins apparently doesn't do what I thought? I
thought that it specified a join table for the query, but apparently
not?
I also tried overriding joins_for_collection, but that also didn't
work. Some of the queries already have the events_venues join table
automatically included, some don't. So when I override
joins_for_collection to add the join table manually, the SQL code is
invalid for those queries where Active Scaffold already added the join
table to the query. So those queries break. But if I remove it, then
the remaining queries will break; the ones that Active Scaffold
doesn't add the join table into.
I'm pretty confused, because it seemed from my research that at least
a few people have managed to get nested scaffolds working with HABTM
associations between models. Should this work in the first place, or
am I trying to do something that isn't possible with Active Scaffold?