# Has\_many :through associated object ids assignment error

**URL:** <https://www.ruby-forum.com/t/has-many-through-associated-object-ids-assignment-error/91018>\
**Category:** Rails\
**Created:** [April 3, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/has-many-through-associated-object-ids-assignment-error/91018 "2007-04-03T21:35:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [April 3, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/has-many-through-associated-object-ids-assignment-error/91018/1 "2007-04-03T21:35:52Z")

</div>

Hi,

I have the following situation:

class Article \< ActiveRecord::Base  
has\_many :readings  
has\_many :users, :through =\> :readings  
end

class Reading \< ActiveRecord::Base  
belongs\_to :article  
belongs\_to :user  
end

Now what I’m trying to do is:

article = Article.new  
article.save!

# user ids 1,2,3 exist in database, as does the article

article.user\_ids = [1, 2, 3]

But I get the following error:

“undefined method `user\_ids=’ for #Article:0x4aea69c”

Why isn’t the “user\_ids=” method created?

Thanks,  
Bas

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [April 3, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/has-many-through-associated-object-ids-assignment-error/91018/2 "2007-04-03T22:20:45Z")

</div>

Bas van Westing wrote:

> class Article \< ActiveRecord::Base  
> has\_many :readings  
> has\_many :users, :through =\> :readings  
> end
> 
> class Reading \< ActiveRecord::Base  
> belongs\_to :article  
> belongs\_to :user  
> end

Seems to me this is not the model you are seeking… perhaps this one:  
class Article \< ActiveRecord::Base  
has\_many :readings  
end

class User \< ActiveRecord::Base  
has\_many :readings  
end

class Reading \< ActiveRecord::Base  
belongs\_to :user # like a regular join table… but there are  
benefits to making it a first class model  
belongs\_to :article  
end

This makes sense to me, an article can be read by many users and a user  
can read many articles making either a join table or a join model  
necccessary…

if you just prefer the link table approach, you can go with this:

class Article \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users, :through=\>:readings  
end

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :articles, :through=\>:readings  
end # remember to set the link table “readings” id=\>false in the  
migration file

> Now what I’m trying to do is:
> 
> article = Article.new  
> article.save!
> 
> # user ids 1,2,3 exist in database, as does the article
> 
> article.user\_ids = [1, 2, 3]
> 
> But I get the following error:
> 
> “undefined method `user\_ids=’ for #Article:0x4aea69c”
> 
> Why isn’t the “user\_ids=” method created?
> 
> Thanks,  
> Bas

with the above model approach, you can do

a = Article.create  
3.times { a.readings \<\< User.create } # let rails assign the ids… it’s  
basically none of your bussiness outside of test cases… 🙂

User.find(:all).count # =\> 3

or with the link table approach, you can do  
a = Article.new  
3.times {a.users \<\< User.create}

none of the above code segments have been tested but it’s the general  
idea… 🙂

hope this helps

ilan

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [April 3, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/has-many-through-associated-object-ids-assignment-error/91018/3 "2007-04-03T23:01:53Z")

</div>

Thanks Ilan for your reply,

I thought that has\_and\_belongs\_to\_many was depreciated (or at least not  
a best practice anymore), since in the “Agile Web development with  
Rails” book it is not advices to use it anymore.

The ActiveRecord join model is said to be the preferred way of working,  
but I still like to use the ‘user\_ids=’ assignment which is allowed in  
the habtm variant.

In my current application, I just defined the missing method myself, so  
I can keep working with the benefits of the habtm approach.

class Article  
def user\_ids=(array = [])  
…  
end  
end

This works for me, but I hoped it would be auto-generated for me by  
using the “has\_many :users, through =\> :readings” DSL line.

Regards, Bas
