Help with has_and_belongs_to_many

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.

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 via http://www.ruby-forum.com/\.

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)

=> #<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

···

On Dec 7, 2:55 am, Peter Bunyan <peter.bun...@gmail.com> wrote:

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

    a.tags << z

···

On 12/7/07, Junkone <junkone1@gmail.com> wrote:

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/

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

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

···

On Dec 7, 9:45 am, Rick DeNatale <rick.denat...@gmail.com> wrote:

On 12/7/07, Junkone <junko...@gmail.com> wrote:

> 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/

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

    a.tags << z

--
Rick DeNatale

My blog on Rubyhttp://talklikeaduck.denhaven2.com/

a.tags.delete(z)

···

On 12/7/07, Junkone <junkone1@gmail.com> wrote:

On Dec 7, 9:45 am, Rick DeNatale <rick.denat...@gmail.com> wrote:

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

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

thanks a lot.

···

On Dec 7, 12:05 pm, Rick DeNatale <rick.denat...@gmail.com> wrote:

On 12/7/07, Junkone <junko...@gmail.com> wrote:

> On Dec 7, 9:45 am, Rick DeNatale <rick.denat...@gmail.com> wrote:
> > 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.

          a.tags.delete(z)

--
Rick DeNatale

My blog on Rubyhttp://talklikeaduck.denhaven2.com/