Rails 三个对象关联问题

stephen · December 16, 2011 · Last by yorzi replied at February 04, 2012 · 2799 hits

有三个 model, a, b , c!!! a belongs_to b , a belongs_to c! b has_many a, c has_many a!

当 a create 的时候,我用 b.a.build 创建对象的,而 c 呢?是不是只能通过传递参数 params[:c_id] 来传值???

三个对象又不是 串联,相当于 ' a has_many_and_belongs_to b a has_many_and_belongs_to c ' c.a.build params[:c_id]

a, b, c 看得都晕了,用实际点得名字大家更容易看明白 我这里顺便提醒一下 如果你希望你的提问能有更多人感兴趣,那就好好的把排版整理一下,描述也要细致一些,多为看的人考虑,不然很容易被别人忽略掉的。 问问题得有诚意嘛

1,2 樓的同學似乎根本沒仔細看 lz 的問題就匆忙回答了啊

B 和 c 有关系么,没关系看上去象 polymorphic association. Blog has many photo, Page has many photo, Photo belongs to blog, Photo belongs to page.

#4 楼 @Victor 说 no 的同时请说 why no。高见?

#6 楼 @sandy_xu 首先,盡量避免使用 has_many_and_belongs_to,改用 has_many,有關這個問題網上已經有很多討論,google 一下能找不到不少相關文章。

另外我覺得 LZ 的場景實在沒必要使用 has_many_and_belongs_to。你可以仔細看看 #5 楼 @hisea 的回帖。而二樓給出的 has_many :through 更是跟 LZ 的面臨的問題無關。

#6 楼 @sandy_xu 如果我最初回帖的語氣讓你覺得不舒服,我想說“請原諒,今後我會試著委婉一些”

You need to Sign in before reply, if you don't have an account, please Sign up first.