# Has\_many through association

**URL:** <https://www.ruby-forum.com/t/has-many-through-association/167417>\
**Category:** Rails\
**Created:** [May 7, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/has-many-through-association/167417 "2009-05-07T17:49:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![intern\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@intern\_ruby](https://www.ruby-forum.com/u/intern_ruby)\
**Post date:** [May 7, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/has-many-through-association/167417/1 "2009-05-07T17:49:59Z")

</div>

1))))) class Group \< ActiveRecord::Base

has\_many :group\_users  
has\_many :users, :through =\> :group\_users  
end

2)))))))))class User \< ActiveRecord::Base  
has\_many :group\_users  
has\_many :groups, :through =\> :group\_users  
end

3))))))class CreateGroupUsers \< ActiveRecord::Migration # JOINING  
TABLE  
def self.up  
create\_table :group\_users do |t|  
t.column :group\_id, :string  
t.column :user\_id, :string  
t.column :group\_admin, :boolean  
t.timestamps  
end  
end

Can anybody please help me how to set the value of group\_admin in  
GroupUsers ?

I want to do like this :

@group.group\_users.group\_admin =1 (X- NOT WORKING THOUGH)

---

<div class="post-metadata">

**Author:** ![intern\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@intern\_ruby](https://www.ruby-forum.com/u/intern_ruby)\
**Post date:** [May 7, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/has-many-through-association/167417/2 "2009-05-07T18:46:01Z")

</div>

> @group.group\_users.group\_admin =1 (X- NOT WORKING THOUGH)

@group.group\_users is not a single entity with the group\_admin  
attribute, it’s a collection of group\_users records…

@group.group\_users.each do |group\_user|  
group\_user.group\_admin = 1  
end

would set that attribute for every group\_user record, but I don’t think  
that is you want to do.

I’d think you want to assign the group\_admin for a single group\_users  
record for the group, which you’ll need to select according to the  
user\_id.

A particularly ugly, but very clear implementation, might be:

@group.group\_users.each do |group\_user|  
if group\_user.user\_id == the\_user\_id\_of\_your\_admin  
group\_user.group\_admin = 1  
end  
end

---

<div class="post-metadata">

**Author:** ![intern\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@intern\_ruby](https://www.ruby-forum.com/u/intern_ruby)\
**Post date:** [May 7, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/has-many-through-association/167417/3 "2009-05-07T18:49:27Z")

</div>

Intern ruby wrote:

> Can anybody please help me how to set the value of group\_admin in  
> GroupUsers ?
> 
> I want to do like this :
> 
> @group.group\_users.group\_admin =1 (X- NOT WORKING THOUGH)

@group.group\_users contains an array of users… so you need to specify  
what user to set group\_admin=1

try to open script/console and do:

> test = Group.find(1)  
> test.group\_users  
> test.group\_users[0]  
> test.group\_users[0].group\_admin=1  
> –  
> ===============================================================================  
> Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![intern\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@intern\_ruby](https://www.ruby-forum.com/u/intern_ruby)\
**Post date:** [May 7, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/has-many-through-association/167417/4 "2009-05-07T19:32:02Z")

</div>

thanks Chron and Tom. It worked.
