# Cannot associate new records

**URL:** <https://www.ruby-forum.com/t/cannot-associate-new-records/124869>\
**Category:** Rails\
**Created:** [January 3, 2008, 11:00am UTC](https://www.ruby-forum.com/t/cannot-associate-new-records/124869 "2008-01-03T11:00:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [January 3, 2008, 11:00am UTC](https://www.ruby-forum.com/t/cannot-associate-new-records/124869/1 "2008-01-03T11:00:44Z")

</div>

After looking at a list of feeds, clicking on a specific feed gives an  
interface to assign a tag to that feed with checkboxes. So far so good,  
but [http://localhost:3000/feeds/add\_some\_tags/1](http://localhost:3000/feeds/add_some_tags/1) gives:

ActiveRecord::HasManyThroughCantAssociateNewRecords in  
FeedsController#add\_some\_tags

Cannot associate new records through ‘Feed#colors’ on ‘#’. Both records  
must have an id in order to create the has\_many :through record  
associating them.

That’s all well and good, but both records do have an id, of course.  
However, how do I get those id’s and put them into the join table  
(colors)? Now, this is getting interesting because it’s starting to  
deal  
with the db directly. The models are at: [http://strawr.googlecode.com/](http://strawr.googlecode.com/)  
svn/trunk/app/models/ and look fine to me.

Part of the controller:

thufir@arrakis ~/Desktop/strawr $  
thufir@arrakis ~/Desktop/strawr $ tail app/controllers/  
feeds\_controller.rb -n 20

def add\_some\_tags  
@feed = Feed.find(params[:id])  
@unused\_tags =  
Tag.find(:all) - @feed.tags

if @unused\_tags.any?  
@tags\_to\_add = @unused\_tags.select { |tag|  
(@params[‘tag’+tag.id.to\_s][‘checked’] == ‘1’)}  
@tags\_to\_add.each { |tag|  
@feed.tags \<\< Tag.find\_or\_create\_by\_tag(params[:which\_tag])}  
end

if @tags\_to\_add.any? and @feed.save  
flash[:notice] = ‘Tags have been added!’  
end  
redirect\_to :action =\> ‘show’, :id =\> @feed  
end

end  
thufir@arrakis ~/Desktop/strawr $

thanks,

Thufir
