how to update a table which is connected with joins

12 views
Skip to first unread message

honey ruby

unread,
Jun 12, 2012, 5:31:08 AM6/12/12
to rubyonra...@googlegroups.com

hi all there are two tables Student and Batch in there model i have
has_and_belongs_to_many :graduated_batches, :class_name => 'Batch', :join_table => 'batch_students' in Student table and
 has_and_belongs_to_many :graduated_students, :class_name => 'Student', :join_table => 'batch_students' in Batch table
which are connected to a table called "batch_students"
i have added a new column to batch_students table and i want to update the value in batch_students table. How can i update values in batch_students table where there is no model for that table so that i can update directly.how to update table which is in joins.

thanks in advance

Michael Pavling

unread,
Jun 12, 2012, 5:32:51 AM6/12/12
to rubyonra...@googlegroups.com
On 12 June 2012 10:31, honey ruby <emailtoh...@gmail.com> wrote:
>
> hi all there are two tables Student and Batch in there model i have
> has_and_belongs_to_many :graduated_batches, :class_name => 'Batch',
> :join_table => 'batch_students' in Student table
> i have added a new column to batch_students table and i want to update the
> value in batch_students table. How can i update values in batch_students
> table where there is no model for that table so that i can update
> directly.how to update table which is in joins.


Sounds like you want to change those habtm relationships to "has_many
:through => :batch_students"

hanish jadala

unread,
Jun 12, 2012, 5:37:02 AM6/12/12
to rubyonra...@googlegroups.com
no with our changing the relationship can i update values.


thanks in advance


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


hanish jadala

unread,
Jun 13, 2012, 3:31:03 AM6/13/12
to rubyonra...@googlegroups.com
hi all help me out guys


thanks in advance

Damjan Rems

unread,
Jun 13, 2012, 4:20:26 AM6/13/12
to rubyonra...@googlegroups.com
How about:

http://abdul-barek-rails.blogspot.com/2009/12/how-to-execute-sql-query-in-rails.html

I didn't tried it, but you may probably write Students.execute('update
anytable set .....')

by
TheR

--
Posted via http://www.ruby-forum.com/.

Michael Pavling

unread,
Jun 13, 2012, 4:21:02 AM6/13/12
to rubyonra...@googlegroups.com
On 13 June 2012 08:31, hanish jadala <emailtoh...@gmail.com> wrote:
> hi all help me out guys
>
>
> thanks in advance
>
> On Tue, Jun 12, 2012 at 3:07 PM, hanish jadala <emailtoh...@gmail.com>
> wrote:
>>
>> no with our changing the relationship can i update values.


We did - you refused to do it :-/

Let's just restate your position: You want to have a habtm join, with
extra information in the join table. This type of join typically does
not have a model, and thus no way of updating those extra fields
(although a "feature" of Rails does/did extend columns in the join
table to both sides of the association).
You don't want to add a new model and join through it (the way it
would be done normally).
So you see the circular problem you have...

Let's take one step back; rather than demanding that people "help you
out", maybe you can explain *why* you don't want to change your
association...

hanish jadala

unread,
Jun 13, 2012, 5:20:33 AM6/13/12
to rubyonra...@googlegroups.com
thanks for your reply guys. hi Michael Pavling i can not change my relationship because i have used in many other places if i change  relationship i have to make more changes in my app that is y i can not change my relationship.


thanks in advance


Michael Pavling

unread,
Jun 13, 2012, 5:26:53 AM6/13/12
to rubyonra...@googlegroups.com
On 13 June 2012 10:20, hanish jadala <emailtoh...@gmail.com> wrote:
> hi Michael Pavling i can not change my
> relationship because i have used in many other places if i change
> relationship i have to make more changes in my app that is y i can not
> change my relationship.

So instead of doing a refactor to support the new functionality you
want (a moderately fiddly job to change "many other places" - but an
opportunity to DRY them up...), you're going to plan to bodge in
something that will make it practically impossible to ever refactor
this area of code in the future. This will make any future
functionality changes in this area an absolute nightmare.

Google "technical debt".
Reply all
Reply to author
Forward
0 new messages