Belongs_to :through

hi there,
lets say you have: A that belongs_to :b, and B belongs_to :c

how do you normally go about saying A belongs_to :c, :through => :b ??

searching I found two solutions:

  1. method:
    class A
    def c
    b.c
    end
    end

and

2.delegate:
class A
delegate :c, :to => :b
end

but neither will have the benefits of generated sql with joins.

How you normally do this? and What are the reasons against having it on
rails itself?

greetings
joaquin


www.least-significant-bit.com

Joaquin Rivera padron wrote:

hi there,
lets say you have: A that belongs_to :b, and B belongs_to :c

how do you normally go about saying A belongs_to :c, :through => :b ??

If A belongs_to :b, that means that Rails expects a b_id field in A’s
table. In your case, that doesn’t exist, so what you want is
A has_one :c, :through => :b

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Joaquin Rivera padron wrote:

hi there,
lets say you have: A that belongs_to :b, and B belongs_to :c

how do you normally go about saying A belongs_to :c, :through => :b ??

If A belongs_to :b, that means that Rails expects a b_id field in A’s
table. In your case, that doesn’t exist, so what you want is
A has_one :c, :through => :b

Wow, that was stupid of me. I must not have been awake when I wrote
that. has_one :through will probably work, but my explanation is
totally inaccurate. Sorry.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs