Coder Social home page Coder Social logo

Comments (8)

adlerhsieh avatar adlerhsieh commented on May 20, 2024

看起來你是想要解決某個作業或指定的問題?

Model的實作算是Rails當中很基本但也很複雜的部份,從頭到尾描述的話可能寫3個小時也寫不完,而且網路上已經有非常多此類教材,不管收費與否都非常專業,建議參考網路上的現成教材,只要整個操作過一遍,要解開這兩題就不是難事了。

教材部分建議可以從實戰聖經的這篇Active Record基本說明入手,對於Active Record說明非常清楚,如果有點無法理解,我之前有寫過一篇尋找教材的參考,也可以參考一下。

from ama.

adlerhsieh avatar adlerhsieh commented on May 20, 2024

另外幫你稍微整理一下貼code方式,有其他人想回答的話應該比較快就catch到意思 :)

from ama.

minjindang avatar minjindang commented on May 20, 2024

感謝,先說明這不是作業,目的是要挑戰自己能不能活用Model
自己有嘗試先建三個Model分別是Person Children Grandchildren
後來發現這樣行不通...

看了Active Record有很多部分,不知道哪個部分才是適合的?
是否能給一個明確方向,不是只是大方向

補充:可能會有程式碼所以打在gist上讓畫面簡潔一點

from ama.

sunkai612 avatar sunkai612 commented on May 20, 2024

可以使用 self join table
http://guides.rubyonrails.org/association_basics.html#self-joins

Person 多開一個欄位 parent_id

children method 就去找 parent_id 為 Person id 的人
grandchildren 則是找 children 的 children 就可以了

from ama.

zackexplosion avatar zackexplosion commented on May 20, 2024

建議貼 Code 的時候貼可以執行的 Code 。 這樣別人比較好 Try Code
https://gist.github.com/zackexplosion/89b56e68729c4c252d7c

from ama.

timfanda35 avatar timfanda35 commented on May 20, 2024

想到了 ActsAsTree gem,或許可以做為參考

from ama.

adlerhsieh avatar adlerhsieh commented on May 20, 2024

@willycode 抱歉,因為我的理解錯誤才以為這是作業的問題,希望沒有造成困擾。

補充一下樓樓樓上的作法:

class Person < ActiveRecord::Base
  has_many   :children, class_name: "User", foreign_key: :parent_id
  belongs_to :parent,   class_name: "User"

  def grandchildren
    self.class.where(parent_id: children.ids)
  end
end

from ama.

minjindang avatar minjindang commented on May 20, 2024

感謝大家:)

from ama.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.