# Push\_with\_attributes for a has\_many\_through

**URL:** <https://www.ruby-forum.com/t/push-with-attributes-for-a-has-many-through/225986>\
**Category:** Rails\
**Created:** [January 2, 2008, 10:42am UTC](https://www.ruby-forum.com/t/push-with-attributes-for-a-has-many-through/225986 "2008-01-02T10:42:48Z")\
**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 2, 2008, 10:42am UTC](https://www.ruby-forum.com/t/push-with-attributes-for-a-has-many-through/225986/1 "2008-01-02T10:42:48Z")

</div>

I’m basing feeds/show.rhtml and feeds\_controller.rb on

Chapter 12 - More Model Magic  
Ruby on Rails For Dummies  
by Barry B.  
John Wiley & Sons © 2007

specifically listings 12-12 and 12-14. Of course, it’s now deprecated  
☹

There’s a has\_many\_through between the “feeds” and “tags” tables. The  
join table is “colors”, for lack of a better name. I believe that the  
name of the join table and that it’s a has\_many\_through are causing the  
error message.

I have a screen shot at [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/strawr/) , just click on  
the add\_a\_tag.png. Up to that point, everything works. However, when I  
click on the button I get the following error:

NoMethodError in FeedsController#add\_some\_tags

undefined method `push\_with\_attributes’ for Tag:Class

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
base.rb:1238:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ associations/has_many_through_association.rb:120:in `send’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
associations/has\_many\_through\_association.rb:120:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ base.rb:946:in `with\_scope’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
associations/has\_many\_through\_association.rb:120:in `method_missing' /home/thufir/Desktop/strawr/app/controllers/feeds_controller.rb:88:in `add\_some\_tags’  
/home/thufir/Desktop/strawr/app/controllers/feeds\_controller.rb:87:in  
`each' /home/thufir/Desktop/strawr/app/controllers/feeds_controller.rb:87:in `add\_some\_tags’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
base.rb:1238:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ associations/has_many_through_association.rb:120:in `send’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
associations/has\_many\_through\_association.rb:120:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ base.rb:946:in `with\_scope’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
associations/has\_many\_through\_association.rb:120:in `method_missing' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ base.rb:1101:in `send’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
base.rb:1101:in `perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ filters.rb:696:in `call\_filters’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
filters.rb:688:in `perform_action_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ benchmarking.rb:66:in `perform\_action\_without\_rescue’  
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ benchmarking.rb:66:in `perform\_action\_without\_rescue’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
rescue.rb:83:in `perform_action' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ base.rb:435:in `send’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
base.rb:435:in `process_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ filters.rb:684:in `process\_without\_session\_management\_support’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
session\_management.rb:114:in `process' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ base.rb:334:in `process’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb:41:in  
`dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick_server.rb:113:in `handle\_dispatch’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick\_server.rb:79:in  
`service' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service’  
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/lib/ruby/1.8/webrick/server.rb:173:in `start\_thread’  
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start' /usr/lib/ruby/1.8/webrick/server.rb:162:in `start\_thread’  
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/lib/ruby/1.8/webrick/server.rb:92:in `each’  
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/lib/ruby/1.8/webrick/server.rb:23:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick_server.rb:63:in `dispatch’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/servers/webrick.rb:59  
/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’  
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/  
dependencies.rb:495:in `require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/ dependencies.rb:342:in `new\_constants\_in’  
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/  
dependencies.rb:495:in `require' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/server.rb:39 /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem\_original\_require’  
/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in `require’  
script/server:3

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
base.rb:1238:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ associations/has_many_through_association.rb:120:in `send’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
associations/has\_many\_through\_association.rb:120:in `method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ base.rb:946:in `with\_scope’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/  
associations/has\_many\_through\_association.rb:120:in `method_missing' /home/thufir/Desktop/strawr/app/controllers/feeds_controller.rb:88:in `add\_some\_tags’  
/home/thufir/Desktop/strawr/app/controllers/feeds\_controller.rb:87:in  
`each' /home/thufir/Desktop/strawr/app/controllers/feeds_controller.rb:87:in `add\_some\_tags’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
base.rb:1101:in `send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ base.rb:1101:in `perform\_action\_without\_filters’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
filters.rb:696:in `call_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ filters.rb:688:in `perform\_action\_without\_benchmark’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
benchmarking.rb:66:in `perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in `measure’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
benchmarking.rb:66:in `perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ rescue.rb:83:in `perform\_action’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
base.rb:435:in `send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ base.rb:435:in `process\_without\_filters’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
filters.rb:684:in `process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/ session_management.rb:114:in `process’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/  
base.rb:334:in `process' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb:41:in `dispatch’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick\_server.rb:113:in  
`handle_dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick_server.rb:79:in `service’  
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’  
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:162:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:95:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' /usr/lib/ruby/1.8/webrick/server.rb:92:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start' /usr/lib/ruby/1.8/webrick/server.rb:82:in `start’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick\_server.rb:63:in  
`dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/servers/webrick.rb:59 /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem\_original\_require’  
/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in `require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/ dependencies.rb:495:in `require’  
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/  
dependencies.rb:342:in `new_constants_in' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/ dependencies.rb:495:in `require’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/server.rb:39  
/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’  
script/server:3

Request

Parameters: {“tag1”=\>{“checked”=\>“0”}, “commit”=\>“Save changes”,  
“tag2”=\>  
{“checked”=\>“0”}, “tag3”=\>{“checked”=\>“1”}, “id”=\>“4”}

Show session dump

* * *

flash: !map:ActionController::Flash::FlashHash {}

Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

The relevant code:

thufir@arrakis ~/Desktop/strawr $  
thufir@arrakis ~/Desktop/strawr $ cat  
app/controllers/feeds\_controller.rb  
class FeedsController \< ApplicationController  
def index  
list  
render :action =\> ‘list’  
end

# GETs should be safe (see [http://www.w3.org/2001/tag/doc/](http://www.w3.org/2001/tag/doc/)

whenToUseGet.html)  
verify :method =\> :post, :only =\> [:destroy, :create, :update],  
:redirect\_to =\> { :action =\> :list }

def list  
@feed\_pages, @feeds = paginate :feeds, :per\_page =\> 10  
end

def show  
@feed = Feed.find(params[:id])  
end

def new  
@feed = Feed.new  
@tag = Tag.find\_all  
end

def create  
@feed = Feed.new(params[:feed])  
if @feed.save  
flash[:notice] = ‘Feed was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def edit  
@feed = Feed.find(params[:id])  
@tag = Tag.find\_all  
end

def update  
@feed = Feed.find(params[:id])  
if @feed.update\_attributes(params[:feed])  
flash[:notice] = ‘Feed was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @feed  
else  
render :action =\> ‘edit’  
end  
end

def destroy  
Feed.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end

#courtesy of Listing 12-12  
#of Ruby on Rails For Dummies

def remove\_tag  
@feed = Feed.find(params[:id])  
@feed.tags.delete(  
Tag.find(params[:which\_tag]))

if @feed.save  
flash[:notice] = ‘Tag has been removed.’  
end  
redirect\_to :action =\> ‘show’, :id =\> @feed  
end

def add\_tag  
@feed = Feed.find(params[:id])  
@feed.tags.push\_with\_attributes(  
Tag.find(params[:tag][:id]))

if @feed.save  
flash[:notice] = ‘Tag has been added!’  
end  
redirect\_to :action =\> ‘show’, :id =\> params[:id]  
end

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.push\_with\_attributes(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 $  
thufir@arrakis ~/Desktop/strawr $ cat app/views/feeds/show.rhtml  
\<% for column in Feed.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @feed.send(column.name) %\>

\<% end %\>

\<%= link\_to ‘Edit’, :action =\> ‘edit’, :id =\> @feed %\> |  
\<%= link\_to ‘Back’, :action =\> ‘list’ %\>

\<% for column in Feed.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @feed.send(column.name) %\>

\<% end %\>

**Tags:**

\<% if @feed.tags.empty? %\>  
(None)

\<% else %\>  
\<% for tag in @feed.tags %\>  
\<%= tag.name %\>&nbsp;  
\<%= link\_to ‘Remove’, { :action =\> ‘remove\_tag’,  
:id =\> @feed, :which\_tag =\> tag.id },  
:confirm =\> ‘Are you sure?’, :post =\> true %\>  
  
  
\<% end %\>  
\<% end %\>

\<% @unused\_tags =  
Tag.find(:all) - @feed.tags %\>

\<% if @unused\_tags.any? %\>

\<%= form\_tag :action =\> “add\_some\_tags”,  
:id =\> @feed %\>  
**Add a tag:**  
  
\<% @unused\_tags.each {|tag| %\>  
\<%= check\_box(‘tag’+tag.id.to\_s, ‘checked’) +  
tag.tag %\>  
  
\<% } %\>  
\<%= submit\_tag %\>  
\<%= end\_form\_tag %\>  
\<% end %\>

\<%= link\_to ‘Edit’, :action =\> ‘edit’, :id =\> @feed %\> |  
\<%= link\_to ‘Back’, :action =\> ‘list’ %\>

thufir@arrakis ~/Desktop/strawr $

thanks,

Thufir
