help with has_and_belongs_to_many

J

Junkone

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.
 
P

Peter Bunyan

Junkone said:
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]
 
J

Junkone

Junkone said:
class Trade < ActiveRecord::Base
has_and_belongs_to_many :tags
class Tag < ActiveRecord::Base
set_table_name "tags"
has_and_belongs_to_many :trades

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]

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=> #<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"}>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
 
R

Rick DeNatale

i get error when assigning many to many relationship
=> #<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"}>
NoMethodError: undefined method `each' for #<Tag:0x4779044>
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/

You don't replace a habtm relationship with an assignment, you add to it

a.tags << z
 
J

Junkone

You don't replace a habtm relationship with an assignment, you add to it

a.tags << z

thanks. how do you do the reverse. if i want to remove an existing
relationship.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,798
Messages
2,569,651
Members
45,384
Latest member
GOLDY

Latest Threads

Top