From: Junkone Date: 2007-12-07T20:30:08+09:00 Subject: Re: help with has_and_belongs_to_many On Dec 7, 2:55 am, Peter Bunyan wrote: > Junkone wrote: > > class Trade < ActiveRecord::Base > > > has_and_belongs_to_many :tags > > > class Tag < ActiveRecord::Base > > set_table_name "tags" > > has_and_belongs_to_many :trades > > > end > > > how to i add records to the table tags_trades using active rcord. i > > dont have a model for tags_trades becaues the docs states that i > > dont need one. > > > thanks in advance. > > Well, first off, you forgot to end the Trade class. And you forgot to > give it a table name. > > Once you've done that, and you've created your instance of the Trade > class (let's call it trade, just to be simple...) and a bunch of Tag > classes with silly names: > > trade.tags = [cheese, timeshare, lubricant] > -- > Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text - i tried that but does not work. i do have the table defined CREATE TABLE `tags_trades` ( `tag_id` int(11) NOT NULL, `trade_id` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; i get error when assigning many to many relationship >> a=Trade.find(1) => #"O", "size"=>nil, "mfe"=>nil, " tradestrategy"=>nil, "symbol_alias"=>"PRGX", "tradeinterval"=>"DAILY", "getpictu res"=>nil, "tradename"=>nil, "id"=>"1", "mae"=>nil, "grossrevenue"=>nil, "tradec omments"=>nil, "tradedirection"=>"Short"}> >> z=Tag.find(1) => #"1", "description"=>"exampe1"}> >> a.tags=z NoMethodError: undefined method `each' for # from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_recor d/base.rb:1863:in `method_missing' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_recor d/associations/association_collection.rb:141:in `replace' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_recor d/associations.rb:962:in `tags=' from (irb):37