gpt4 book ai didi

ruby-on-rails - 如何在 Ruby on Rails 中加入嵌套的 belongs_to

转载 作者:数据小太阳 更新时间:2023-10-29 08:31:13 24 4
gpt4 key购买 nike

我有三个模型,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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com