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:
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)
=> #<Trade:0x477f4bc @attributes={“tradestatus”=>“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)
=> #<Tag:0x4779044 @attributes={“id”=>“1”, “description”=>“exampe1”}>
a.tags=z
NoMethodError: undefined method each' for #<Tag:0x4779044> 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