# How to update the extra field of relationship table when use have\_many :throuth effectively

**URL:** <https://www.ruby-forum.com/t/how-to-update-the-extra-field-of-relationship-table-when-use-have-many-throuth-effectively/150796>\
**Category:** Rails\
**Created:** [October 13, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/how-to-update-the-extra-field-of-relationship-table-when-use-have-many-throuth-effectively/150796 "2008-10-13T12:49:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![guofeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@guofeng](https://www.ruby-forum.com/u/guofeng)\
**Post date:** [October 13, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/how-to-update-the-extra-field-of-relationship-table-when-use-have-many-throuth-effectively/150796/1 "2008-10-13T12:49:42Z")

</div>

I defined the relationship of two tables as below:

Programme(id,code,name)  
Course(id,code,name)  
ProgarmmeCourse(id,programme\_id,course\_id,status)

class Programme \< ActiveRecord::Base  
has\_many :programme\_courses,:dependent=\>:destroy  
end

class ProgrammeCourse \< ActiveRecord::Base  
belongs\_to :programme  
belongs\_to :course  
end

class Course \< ActiveRecord::Base  
has\_many :programmes,:through=\>:programme\_courses  
end

there are a list of courses belong to a programme on Programme’s update  
page, i can update the status(enable/disable) of course belong to the  
programme or add a new course from a list of course.

how to save the field of status(enable/disable) effectively after save  
Programme?

Could you please give me a sample?  
Thanks for your help

–  
View this message in context:  
[http://www.nabble.com/How-to-update-the-extra-field-of-relationship-table-when-use-have\_many-%3Athrouth-effectively-tp19952307p19952307.html](http://www.nabble.com/How-to-update-the-extra-field-of-relationship-table-when-use-have_many-%3Athrouth-effectively-tp19952307p19952307.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![guofeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@guofeng](https://www.ruby-forum.com/u/guofeng)\
**Post date:** [October 13, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/how-to-update-the-extra-field-of-relationship-table-when-use-have-many-throuth-effectively/150796/2 "2008-10-13T16:09:12Z")

</div>

What you’re asking is how to set attributes on the join model. Josh  
Susser  
covered one way to do this here:  
[has\_many :through - New on edge: Magic join model creation](http://blog.hasmanythrough.com/2006/8/19/magic-join-model-creation) with  
his  
push\_with\_attributes method on the association. You use it like this:

class Programme \< ActiveRecord::Base  
has\_many :programme\_courses,:dependent =\> :destroy  
has\_many :courses, :through =\> :programme\_courses do  
def push\_with\_attributes(element, attributes = {})  
ProgrammeCourse.send :with\_scope, {:create =\> attributes} do  
self \<\< element  
end  
end  
end  
end

To create the relationship between a Course and a Programme with a  
status  
value on the join model you can use it like this:  
c = Course.find :first  
p = Programme.find :first  
p.courses.push\_with\_attributes(c, :status =\> ENABLED) # Or whatever  
you’re  
setting status to

To set the value on the join model after it’s been created, you have to  
do  
something more like  
ProgrammeCourse.find\_by\_programme\_id\_and\_course\_id(p,  
c).update\_attribute  
:status, DISABLED # Or whatever you’re setting status to

Of course you should refactor these for simplicity in your application.  
There are probably nicer ways to do this nowadays but I’m not sure what  
they  
are.

Cheers,  
Morgan.

2008/10/13 guofeng.ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> class Programme \< ActiveRecord::Base  
> end
> 
> –  
> View this message in context:  
> [http://www.nabble.com/How-to-update-the-extra-field-of-relationship-table-when-use-have\_many-%3Athrouth-effectively-tp19952307p19952307.html](http://www.nabble.com/How-to-update-the-extra-field-of-relationship-table-when-use-have_many-%3Athrouth-effectively-tp19952307p19952307.html)  
> Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> >

## –

## Morgan G. - Just Landed General Tel: +34 91 590 2611 removed\_email\_address@domain.invalid

## [http://www.justlanded.com](http://www.justlanded.com) - Helping people abroad! 30 countries, in up to 8 languages, more to come…
