Editing many-to-many through associations?

4 views
Skip to first unread message

rlaferla

unread,
Aug 29, 2007, 1:22:57 PM8/29/07
to ActiveScaffold : Ruby on Rails plugin
I read that AS currently supports many-to-many through associations as
read-only. When can we expect to be able to edit them? Is there an
AS release forthcoming? Can someone provide a temporary workaround?
It is a serious limitation.

Michael Latta

unread,
Aug 29, 2007, 3:59:27 PM8/29/07
to actives...@googlegroups.com
You can edit :through associations by editing the join table today.
The default UI is not the best, but that can be customized.

You can also hack AS to treat :through associations like has_many,
but there are numerous issues with this. I am currently working
in this mode but leaning to migration to the above with custom UI to
edit :throughs.

Michael

Open individual

unread,
Sep 25, 2007, 3:06:39 PM9/25/07
to ActiveScaffold : Ruby on Rails plugin
Hi everyone.

I have been pretty impressed with active scaffold so far.
But for the first time as mentioned above there is a limitation.
Many to many seems to not be supported.

Can anyone give me an idea as to how to implement this between say
Vehicles and Customers.
They have a many to many relationship.

Has anyone got a working example.

I am pretty desperate and have run out of ideas.

Any help is much appreciated.

Uncle Mike

Open individual

unread,
Sep 25, 2007, 3:50:40 PM9/25/07
to ActiveScaffold : Ruby on Rails plugin
Wow after much searching (3 days)

I have almost but given up of many to many relationships.

This is a massive issue.

AS is absolutely brilliant except for this one glitch.

Does anyone have a working example?
How do I do it?

Open individual

unread,
Sep 26, 2007, 4:18:35 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Day 4 and the frustration continues.

Please if some guru can solve this with a how to I will be like supper
happy.

Regards

Uncle Mike

Kosmas

unread,
Sep 26, 2007, 6:31:03 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Mike,

What's exactly the problem you are having?
Are you trying to edit the customers_vehicles table and you can't?
Can you display the habtm relationship in AS?
I think in this thread they are talking about a through model, which I
believe you don't need if you only have customer and vehicle models
(?)

I am working on a similar one at the moment, but I was trying to find
out how to display the many to many relationship.
After some searching in the forum, i've found something about some
parameters that couldn't see in any of the documents about
the :join_table, :foreign_key and :association_foreign_key in the
has_and_belongs_to_many.
Have you managed to do that?

Open individual

unread,
Sep 26, 2007, 7:40:25 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Hi Kosmas,
thank you so much for replying,

I remember you helped me with the icon change for actions.
Thanks, that worked well.
You and Robin got it working.

OK so here is the deal.

> What's exactly the problem you are having?

Here is a simple example I am trying now between Students and Subjects
to get the principle working.

One Student can have many Subjects
One Subject can have many Students
The Subjects and Students table are related through an intersecting
entity

I have the following tables
students, subjects and an intersecting entity called ss_relationships

In my model for "subject.rb" I have the following under active record

has_many :ss_relationships
has_many :students, :through => :ss_relationships

In my model for "student.rb" I have the following under active record

has_many :ss_relationships
has_many :subjects, :through => :ss_relationships

In the "ss_relationship.rb" I have the following under active record

belongs_to :subjects
belongs_to :students

At the moment this works fine for all edits in the sub nests.

But the moment for example a subject is created initially without a
student it poses a problem later.
So now that we have added a few subjects I browse the subject list.
Fantastic.

I select the specific subject and then select the student section
which drops down to show me a list of students that in my case are
empty.
So within the nest I select create Student and fill out all the
details and select create.

Then I get the infamous 500 error.

The same obviously applies with Student and trying to add a new
subject.

My DB is definitely working because in the DB if I populate it with
data, then I can see it in the list and even edit it and then update
it.

But new creation is not working.

> Are you trying to edit the customers_vehicles table and you can't?

I can edit the data but I can not create new records for the related
data.
eg A new student for a subject

> Can you display the habtm relationship in AS?

This is exactly what I am trying now.

See below what has changed.

In my model for "subject.rb" I have the following under active record

has_many :ss_relationships
has_and_belongs_to_many :students

In my model for "student.rb" I have the following under active record

has_many :ss_relationships
has_and_belongs_to_many :subjects

In the "ss_relationship.rb" I have the following under active record

belongs_to :subjects
belongs_to :students


> I think in this thread they are talking about a through model, which I
> believe you don't need if you only have customer and vehicle models
> (?)

Yes they are only talking about a through model.
I realised this and have been trying the habtm approach

> I am working on a similar one at the moment, but I was trying to find
> out how to display the many to many relationship.

The through model works but only for read.

> After some searching in the forum, i've found something about some
> parameters that couldn't see in any of the documents about
> the :join_table, :foreign_key and :association_foreign_key in the
> has_and_belongs_to_many.

habtm (has_and_belongs_to_many) seems to be the way to go.
But I have not even got the display to work.

> Have you managed to do that?

Busy trying now but with no luck.

On the demo page of active scaffold you can see the code for the
migration
What is interesting to note is that they do no assign a PK to the
tables in the many to many.

If you come right Kosmas please let me know.

Regards
Uncle Mike

Kosmas

unread,
Sep 26, 2007, 8:31:06 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Hi Mike,

Here's what I've tried so far (i'm using your models for the example,
so maybe there's a chance i miss something):

1. My intersection table is called students_subjects (rails convention
for many to many). Because i have created it with a migration, it has
an id
column, which in habtm theory it shouldn't, but it can help (as I
found out later) for editing the table if you have more columns than
only the
subject_id and student_id.
2. Subject.rb model
has_and_belongs_to_many :students, :join_table =>
"students_subjects", :foreign_key =>
"subject_id", :association_foreign_key => "student_id"
3. Student.rb model
has_and_belongs_to_many :subjects, :join_table =>
"students_subjects", :foreign_key =>
"student_id", :association_foreign_key => "subject_id"
4. Create a model for the intersecting table called number_route.rb
class StudentSubject < ActiveRecord::Base
set_table_name "students_subjects"
end
5. Create a controller for the intersecting table called
students_subjects_controller.rb
class StudentsSubjectsController < ApplicationController
active_scaffold :student_subject do |config|
config.list.columns =
[:id, :student_id, :subject_id, :extra_column_a, :extra_column_b]
end
end
6. You should be able to create/edit/delete any of the three tables by
going to each one (ie students/list - subjects/list -
students_subjects/list)

On Sep 26, 12:40 pm, Open individual <michaelschwa...@gmail.com>
wrote:

Open individual

unread,
Sep 26, 2007, 8:54:51 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Nice one Kosmas,

Let me try this out.

I will post my findings and let you know.

I am really excited.


Open individual

unread,
Sep 26, 2007, 9:27:10 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Thanks Kosmas,

Check this out.
I still have the same problem even with the modifications.

Can you try something for me so that I can confirm that it is indeed
my code.

Open Subjects for example.
Create a new subject and do not assign it a Student.
(You might need to edit your create to prevent the display of the
student for create)


Then in the subject list area you should see your subject and in the
field Students you should see a dash (indicating no student)
Now click the dash and then create the new student.

When you select the create button at the bottom do you get an error or
is it creating the student to that subject.

If you get the error then I am no further than I was 4 days ago.

If it goes through then Halleluja I will continue patching up what I
have to make it look like yours.

Mike


Kosmas

unread,
Sep 26, 2007, 9:39:24 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Mike,

I suspect is something to do with :
a) your config.create in the subjects
b) your model relations?

Have you checked to see what the error is?
Have a look at the log file (development.log most probably).

Would you like to post your model code?

Have you taken out the references to has_many , belongs_to ?

I've tried with my models (by the way I've got numbers-routes and
numbers_routes but it's similar), and it seems to be working fine for
me.

Open individual

unread,
Sep 26, 2007, 9:50:14 AM9/26/07
to ActiveScaffold : Ruby on Rails plugin
Cool stuff Kosmas,

Thanks for the reply.

I really appreciate it and want to thank you fro your time.

If the create works on your side when you go to say a specific subject
and then select create a new or additional student.
Then the problem lies on my side.

I will get this going no matter what.

I will also post my findings.

Open individual

unread,
Sep 27, 2007, 4:33:21 AM9/27/07
to ActiveScaffold : Ruby on Rails plugin
Hey Kosmas I want to thank you for your help.

I managed to get it resolved.

I just followed exactly what they had in the demo code from scratch.

Thanks a million.

Select the download code and follow it very strictly and change the
values in the migrate DB section to your DB
http://demo.activescaffold.com/users

This is basically a working version of many to many.

Nice

Regards
Uncle Mike

Kosmas

unread,
Sep 27, 2007, 4:50:47 AM9/27/07
to ActiveScaffold : Ruby on Rails plugin
Glad to hear that Mike :-) :-)

I'm trying to use the tabbed widget at the moment so I'll post to see
if anyone else has used it!


On Sep 27, 9:33 am, Open individual <michaelschwa...@gmail.com> wrote:
> Hey Kosmas I want to thank you for your help.
>
> I managed to get it resolved.
>
> I just followed exactly what they had in the demo code from scratch.
>
> Thanks a million.
>
> Select the download code and follow it very strictly and change the

> values in the migrate DB section to your DBhttp://demo.activescaffold.com/users

Reply all
Reply to author
Forward
0 new messages