[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

help with has_and_belongs_to_many

Junkone

12/7/2007 12:36:00 AM

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

Peter Bunyan

12/7/2007 7:55:00 AM

0

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

Junkone

12/7/2007 11:27:00 AM

0

On Dec 7, 2:55 am, Peter Bunyan <peter.bun...@gmail.com> 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-fo... 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

Rick DeNatale

12/7/2007 2:46:00 PM

0

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/

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

a.tags << z

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Junkone

12/7/2007 3:55:00 PM

0

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

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

Rick DeNatale

12/7/2007 5:05:00 PM

0

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.

a.tags.delete(z)


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Junkone

12/7/2007 5:24:00 PM

0

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

thanks a lot.