所以我想我搞砸了数据库之间的关系。
我有一个日历,我希望已创建游览的用户能够查看游览请求。
另一个用户可以查看他们的旅行,并决定预订他们的旅行。
用户可以预订多个旅游。
我的模型:
用户
旅游
Tour_request
用户.rb
has_many :tours
has_many tour_requests
tour_request.rb
belongs_to :user
belongs_to :tour
旅游网
belongs_to :user
has_many :tour_requests
现在我最初的代码是:
current_user.tour_requests.all
然而,这是错误的,因为要求参观的人也会看到它。我只希望参观的人看到它。因此,我尝试了:
current_user.tours.tour_requests.all
但这给了我一个错误。
我做错了什么?我的联想搞砸了吗?
current_user.tours.tour_requests.all
的问题在于current_user.tours
返回ActiveRecord::Relation
,这没有定义tour_requests
。如果您只需要用户创建的所有游览的所有tour_requests的列表,则需要has_many :through
关联:
用户.rb
has_many :tours
has_many :tour_requests
has_many :requests_for_own_tours, through: :tours, source: :tour_request