Comments (8)
看起來你是想要解決某個作業或指定的問題?
Model的實作算是Rails當中很基本但也很複雜的部份,從頭到尾描述的話可能寫3個小時也寫不完,而且網路上已經有非常多此類教材,不管收費與否都非常專業,建議參考網路上的現成教材,只要整個操作過一遍,要解開這兩題就不是難事了。
教材部分建議可以從實戰聖經的這篇Active Record基本說明入手,對於Active Record說明非常清楚,如果有點無法理解,我之前有寫過一篇尋找教材的參考,也可以參考一下。
from ama.
另外幫你稍微整理一下貼code方式,有其他人想回答的話應該比較快就catch到意思 :)
from ama.
感謝,先說明這不是作業,目的是要挑戰自己能不能活用Model
自己有嘗試先建三個Model分別是Person Children Grandchildren
後來發現這樣行不通...
看了Active Record有很多部分,不知道哪個部分才是適合的?
是否能給一個明確方向,不是只是大方向
補充:可能會有程式碼所以打在gist上讓畫面簡潔一點
from ama.
可以使用 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.
建議貼 Code 的時候貼可以執行的 Code 。 這樣別人比較好 Try Code
https://gist.github.com/zackexplosion/89b56e68729c4c252d7c
from ama.
想到了 ActsAsTree gem,或許可以做為參考
from ama.
@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.
感謝大家:)
from ama.
Related Issues (20)
- fork gem 後指定 gem source 為自己的 github,capistrano deploy bundle 錯誤 HOT 10
- 請教Devise的問題 HOT 1
- 實作 Agile Web Development With Rails 4 HOT 5
- Agile Web Development With Rails 4 Chapter 11.5
- Agile Web Development With Rails 4 Chapter 11.5
- ActionCable 佈署到 production 後 WebSocket connection failed HOT 4
- 如何直接取得hash的size並作運算 HOT 2
- About rails breadcrumbs gem
- Rails5 WebSocket 偶爾 handshake 404 HOT 2
- 請問關於加密的問題 HOT 2
- 面試題目參考資料 HOT 1
- 請問為何 better_errors 無法在 Rails 4.2.0 + Ruby 2.2.0 上使用? HOT 2
- 詢問 space 和 tab 轉換工具 HOT 6
- 請問要如何實做複選框+背景新增值的功能,或是有那種複選tagging的功能? HOT 11
- 為什麼 deep_dup 實作卻只是呼叫 dup HOT 1
- 請教一下,我把專案從Rails 4.2升級到 5.2,忽然Devise就無法登出了!!! HOT 5
- 請教一下,如何讓一個驗證失敗後就停住? HOT 2
- 想請教一下,如何讓事件點進去能在 Daterangepicker 顯示正確日期? HOT 3
- 商务
- Form only works after page refresh (rails 5.2.8.1, jquery 4.5.1) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ama.