# Accessing the join association model

**URL:** <https://www.ruby-forum.com/t/accessing-the-join-association-model/135669>\
**Category:** Rails\
**Created:** [April 24, 2008, 2:44am UTC](https://www.ruby-forum.com/t/accessing-the-join-association-model/135669 "2008-04-24T02:44:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 24, 2008, 2:44am UTC](https://www.ruby-forum.com/t/accessing-the-join-association-model/135669/1 "2008-04-24T02:44:00Z")

</div>

Hi,

I have a many-to-many association implementing using association join  
models:

class Source  
has\_many :source\_users  
has\_many :users, :through =\> :source\_users  
end

class SourceUser  
belongs\_to :source  
belongs\_to :user  
end

class User  
has\_many :source\_users  
has\_many :users, :through =\> :source\_users

The source\_users table also contains a field called source\_admin,  
which marks the current user as an admin for this source. When I  
create a new user with:

some\_source.users \<\< new\_user

, how do I insert “true” or “false” in the source\_admin field of the  
SourceUser record that gets created?

Thanks,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 24, 2008, 6:23am UTC](https://www.ruby-forum.com/t/accessing-the-join-association-model/135669/2 "2008-04-24T06:23:58Z")

</div>

It depends upon how you create the new user. Typically, if the user is  
created via data in a params, you might have something like:

@user = User.new(params[:user])

So, two things. You could do:

@user = User.new(params[:user])  
@user.source\_admin = true  
some\_source.users \<\< @user

(and that last one, I believe will also save it unless it fails  
validation)

Or you could merge the source\_admin with the params:

@user = User.new(params[:user].merge!(:source\_admin =\> true))  
some\_source.users \<\< @user

The first option is better if you need to add some logic hooks, i.e.:

@user.source\_admin = true unless XYZ

HTH!

-Danimal

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 25, 2008, 12:16am UTC](https://www.ruby-forum.com/t/accessing-the-join-association-model/135669/3 "2008-04-25T00:16:44Z")

</div>

Hi,

This is what I ended up doing. I thought there is a funky Rails way to  
do it, but I guess not.

Thanks for the help,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 25, 2008, 7:14am UTC](https://www.ruby-forum.com/t/accessing-the-join-association-model/135669/4 "2008-04-25T07:14:41Z")

</div>

Maybe there’s a way to do it in less lines. But for me at least, if  
it’s clear and it works, why worry? I used to get wrapped around the  
axle over that until I realized that sometimes taking a few lines to  
do something so it’s easy to read and follow is better than doing it  
all in one line.

-Danimal

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 24, 2008, 6:30am UTC](https://www.ruby-forum.com/t/accessing-the-join-association-model/135669/5 "2008-04-24T06:30:59Z")

</div>

Woops! I didn’t read your original post very clearly. I think you are  
having this problem:  
[http://blog.hasmanythrough.com/2006/4/17/join-models-not-proxy-collections](http://blog.hasmanythrough.com/2006/4/17/join-models-not-proxy-collections)

So one way is to simply do:

source = Source.find(id)  
user = User.new(params[:user])  
sourceuser = SourceUser.new(:source =\> source, :user =\>  
user, :source\_admin =\> true)

Then I think just calling:

sourceuser.save

will also save the new user. But you’ll want to double-check that.

Is that what you were looking for?

-Danimal
