作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有三个模型,Application
,它属于一个Board
,它属于一个User
。
class User < ActiveRecord::Base
has_many :boards
end
class Board < ActiveRecord::Base
belongs_to :user
has_many :applications
end
class Application < ActiveRecord::Base
belongs_to :board
end
我总是只想为当前用户展示看板或应用程序。我怎么能说“为当前用户显示当前板的每个应用程序”?基本上如何查询特定父 ID 值的内容。
最佳答案
你应该首先提供当前的用户id和board id。
user = User.find(user_id) #get current user
board = user.boards.find(board_id) #get current board
board.applications #get every application
您可以从 Rails Guide--Active Record Associations 获得更多信息
关于ruby-on-rails - 如何在 Ruby on Rails 中加入嵌套的 belongs_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32834682/
我是一名优秀的程序员,十分优秀!