# Can I save relationship using hasmanythrough

**URL:** <https://www.ruby-forum.com/t/can-i-save-relationship-using-hasmanythrough/91271>\
**Category:** Ruby\
**Created:** [April 6, 2007, 4:56am UTC](https://www.ruby-forum.com/t/can-i-save-relationship-using-hasmanythrough/91271 "2007-04-06T04:56:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack.tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jack.tang](https://www.ruby-forum.com/u/jack.tang)\
**Post date:** [April 6, 2007, 4:56am UTC](https://www.ruby-forum.com/t/can-i-save-relationship-using-hasmanythrough/91271/1 "2007-04-06T04:56:17Z")

</div>

Hi

In the classic hasmanythrough example:

class Group \< ActiveRecord::Base  
has\_many :memberships  
has\_many :users, :through =\> :memberships  
end

class User \< ActiveRecord::Base  
has\_many :memberships  
has\_many :groups, :through =\> :memberships  
end

group1 = Group.new(…)  
group1.users = [User.find\_by\_id(2)]  
group1.save!

it throws the exception: “users=” is one undefined method. So how can  
i save the relationship in hasmanythrough?

---

<div class="post-metadata">

**Author:** ![jack.tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jack.tang](https://www.ruby-forum.com/u/jack.tang)\
**Post date:** [April 6, 2007, 5:55am UTC](https://www.ruby-forum.com/t/can-i-save-relationship-using-hasmanythrough/91271/2 "2007-04-06T05:55:20Z")

</div>

jack.tang wrote:

> ```
> has_many :memberships
> has_many :groups, :through => :memberships
> 
> ```
> 
> end
> 
> group1 = Group.new(…)  
> group1.users = [User.find\_by\_id(2)]  
> group1.save!
> 
> it throws the exception: “users=” is one undefined method. So how can  
> i save the relationship in hasmanythrough?

To save your relationship you might try

(a) flowers

(b) therapy

© the rails list

---

<div class="post-metadata">

**Author:** ![jack.tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jack.tang](https://www.ruby-forum.com/u/jack.tang)\
**Post date:** [April 6, 2007, 10:05am UTC](https://www.ruby-forum.com/t/can-i-save-relationship-using-hasmanythrough/91271/3 "2007-04-06T10:05:47Z")

</div>

On Apr 6, 10:52 am, “jack.tang” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> has_many :memberships
> has_many :groups, :through => :memberships
> 
> ```
> 
> end
> 
> group1 = Group.new(…)  
> group1.users = [User.find\_by\_id(2)]  
> group1.save!
> 
> it throws the exception: “users=” is one undefined method. So how can  
> i save the relationship in hasmanythrough?

Hi Guys.

here is another question – how can I load asscocitated class while  
fetching. say  
User.find\_by\_name(“test”).groups

The error is “NameError: uninitialized constant User::Groups” , my  
rails version is 1.2.3.  
Can you tell me how to enable :through function?

/Jack

---

<div class="post-metadata">

**Author:** ![jack.tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jack.tang](https://www.ruby-forum.com/u/jack.tang)\
**Post date:** [April 7, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/can-i-save-relationship-using-hasmanythrough/91271/4 "2007-04-07T18:28:08Z")

</div>

On 4/5/07, jack.tang [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> has_many :memberships
> has_many :groups, :through => :memberships
> 
> ```
> 
> end

Where’s your Membership model? I think you need something like:

class Membership \< ActiveRecord::Base  
belongs\_to :group  
belongs\_to :user  
end

> group1 = Group.new(…)  
> group1.users = [User.find\_by\_id(2)]  
> group1.save!

> it throws the exception: “users=” is one undefined method. So how can  
> i save the relationship in hasmanythrough?

This has nothing to do with the :through option. The :has\_many  
relationship doesn’t add a setter. Instead you need to use methods on  
the attribute, which is not a normal ruby array but an ActiveRecord  
object which looks like an Array but is doing the right SQL magic  
under the covers.

Some useful methods include:  
group1.users\<\< user # add a user  
group1.users.push(user1, user2, …) add one or more users  
group1.users.replace(user1, …) replace users with one or more  
users  
group1.users.delete(user1, …) delete one or more users.

If you want to create a :has\_many:through: relationship using just the  
two ends, both far ends of the association need to exist on the  
database:

group1 = Group.create(…) # group needs to have been previously saved  
# … represents  
attributes to be set in the group  
group1.users \<\< User.find\_by\_id(2)

Or to create a new user and add it to the group:

group1 = Group.create(…)  
article.users.create!(…)

If you want to have attributes in the membership if you don’t why are  
you using has\_many:through:? Then you need to create the  
intermediate table yourself. So lets’ say the membership is used to  
hold things like when it expires:

membership = Membership.new  
membership.expiration = Time.now.next\_year.to\_date  
membership.group = Group.find\_by\_id(3) # or how ever you get the group  
membership.user = User.find\_by\_username(“Dirk Gently”)  
membership.save

You’ll probably get more help with ActiveRecord questions on more  
rails specific resources:

> **[Ruby on Rails](https://rubyonrails.org)**
>
> A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
