active_scaffold_joins does ... what?

140 views
Skip to first unread message

talknightlife

unread,
Jul 2, 2008, 5:08:52 PM7/2/08
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?

iUser59

unread,
Jul 23, 2008, 8:19:55 AM7/23/08
to ActiveScaffold : Ruby on Rails plugin
I have a similar problem too.

I have two models with habtm associations like you. if I change habtm
for has_many :through, I can't edit my records.
But if I use habtm I have this error
Mysql::Error: Unknown column 'videos_vlogiciels.video_id' in 'where
clause': SELECT DISTINCT `vlogiciels`.id FROM `vlogiciels` WHERE
(videos_vlogiciels.video_id = '808') ORDER BY vlogiciels.`id` ASC
LIMIT 0, 40

which I don't have with Has_many :through.

Please help me

talknightlife

unread,
Jul 30, 2008, 2:52:25 PM7/30/08
to ActiveScaffold : Ruby on Rails plugin
I'd love to help you but I never got past this problem. I was
thinking about changing to a has_many :through if it might work.

talknightlife

unread,
Aug 7, 2008, 7:12:09 PM8/7/08
to ActiveScaffold : Ruby on Rails plugin
iUser59, you said that when you use has_many :through, you can't edit
your records. I'm trying to use nested scaffolds both ways, with
HABTM and also with has_many :through, and I think that I'm seeing
exactly the same thing that you are.

With HABTM, I get the unknown column error from the join table. With
has_many :through, I get an error:

NoMethodError (undefined method `customer_group=' for #<Class:
0x1f50964>):

"customer_group" is the name of the parent in the has_many :through
relationship. Active Scaffold seems to be looking for a literal
column, and failing.

There is one place in my app where I can just change the model around
so that I use a plain old has_many/belongs_to relationship, which
Active Scaffold can handle. There are other places in my app where
that's just not possible, it has to be either a HABTM or a
has_many :through.

So the big question is: can Active Scaffold handle nested scaffolds
using either HABTM or has_many :through associations? Are we doing
something wrong, or is it just not capable of doing this?

nicolae.claudius

unread,
Aug 10, 2008, 7:23:56 AM8/10/08
to ActiveScaffold : Ruby on Rails plugin
i have the same problem. moreover nested association overall seems to
have problems... might be a rails 2.1 problem ...

dschruth

unread,
Aug 20, 2008, 9:37:50 PM8/20/08
to ActiveScaffold : Ruby on Rails plugin
I'm getting similar problems with HABTM... the links between the two
HABTM related tables do not work.

Philipp

unread,
Sep 6, 2008, 8:11:29 AM9/6/08
to ActiveScaffold : Ruby on Rails plugin
hello,
got it to work ! (again)
i found this hint 3 month ago some where in the web, but today i
reinstalled my rails and had the same problem as u have ...
i think this sollution is not nice but its working ;-)

patch your rails 2.1
/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/
associations.rb

modify around line 1640

def join_for_table_name(table_name)
- #@joins.select{|j|j.aliased_table_name == table_name.gsub(/
^\"(.*)\"$/){$1} }.first rescue nil
+ join = (@joins.select{|j|j.aliased_table_name ==
table_name.gsub(/^\"(.*)\"$/){$1} }.first) rescue nil
+ return join unless join.nil?
+ @joins.select{|j|j.is_a?(JoinAssociation) &&
j.aliased_join_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first
rescue nil
end

hope this helps

regards
Philipp

dschruth

unread,
Sep 20, 2008, 9:03:04 PM9/20/08
to ActiveScaffold : Ruby on Rails plugin
This fix worked for me thanks!

On Sep 6, 5:11 am, Philipp <philipp.goetzin...@googlemail.com> wrote:
> hello,
> got it to work ! (again)
> i found this hint 3 month ago some where in the web, but today i
> reinstalled my rails and had the same problem as u have ...
> i think this sollution is not nice but its working ;-)
>
> patch your rails 2.1
> /lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/
> associations.rb
>
> modify around line 1640
>
>          def join_for_table_name(table_name)
> -           #...@joins.select{|j|j.aliased_table_name == table_name.gsub(/
Reply all
Reply to author
Forward
0 new messages