通过四个模特有关系吗?

发布于 2024-10-09 13:10:54 字数 1819 浏览 0 评论 0原文

我在模型之间建立这种关联时遇到问题。

一个用户有多个住宿,而住宿有一个用户。

住宿有许多通知,而通知有一种住宿。

请求有很多通知。

我怎样才能获得给定用户的所有请求(即用户 -> 住宿(每个) -> 通知 -> 请求)?

更新:

这是我当前的控制器文件:

class PanelController < ApplicationController

  before_filter :login_required

  def index
    @accommodations = current_user.accommodations.all
    @requests = Array.new
    @accommodations.each do |a|
      a.notifications.each do |n|
        @requests << Request.where('id' => n.request_id)
      end
    end

  end

end

和模型:

models/user.rb

class User < ActiveRecord::Base
  [snip]
  has_many :accommodations
  has_many :notifications,
           :through => :accommodations
end

models/accommodation.rb

class Accommodation < ActiveRecord::Base
  validates_presence_of :title, :description, :thing, :location, :spaces, :price, :photo
  attr_accessible :photo_attributes, :title, :description, :thing, :location, :spaces, :price
  has_one :photo
  has_many :notifications
  belongs_to :user
  accepts_nested_attributes_for :photo, :allow_destroy => true
end

models/notification.rb

class Notification < ActiveRecord::Base
  attr_accessible :accommodation_id, :request_id
  has_one :request
  belongs_to :accommodation
end

模型/request.rb

class Request < ActiveRecord::Base
  belongs_to :notifications
  attr_accessible :firstname, :lastname, :email, :phone, :datestart, :dateend, :adults, :children, :location, :status
  validates_presence_of :firstname, :lastname, :email, :phone, :datestart, :dateend, :children, :adults, :location
end

I'm having trouble setting up this association between my models.

A User has many Accommodations, and Accommodations have one User.

Accommodations have many Notifications, and Notifications have one Accommodation.

Requests have many Notifications.

How can I make it so that I can get all of the Requests for a given User ( that is, User -> Accommodations (each) -> Notification -> Request)?

Update:

Here's my current controller file:

class PanelController < ApplicationController

  before_filter :login_required

  def index
    @accommodations = current_user.accommodations.all
    @requests = Array.new
    @accommodations.each do |a|
      a.notifications.each do |n|
        @requests << Request.where('id' => n.request_id)
      end
    end

  end

end

And models:

models/user.rb

class User < ActiveRecord::Base
  [snip]
  has_many :accommodations
  has_many :notifications,
           :through => :accommodations
end

models/accommodation.rb

class Accommodation < ActiveRecord::Base
  validates_presence_of :title, :description, :thing, :location, :spaces, :price, :photo
  attr_accessible :photo_attributes, :title, :description, :thing, :location, :spaces, :price
  has_one :photo
  has_many :notifications
  belongs_to :user
  accepts_nested_attributes_for :photo, :allow_destroy => true
end

models/notification.rb

class Notification < ActiveRecord::Base
  attr_accessible :accommodation_id, :request_id
  has_one :request
  belongs_to :accommodation
end

models/request.rb

class Request < ActiveRecord::Base
  belongs_to :notifications
  attr_accessible :firstname, :lastname, :email, :phone, :datestart, :dateend, :adults, :children, :location, :status
  validates_presence_of :firstname, :lastname, :email, :phone, :datestart, :dateend, :children, :adults, :location
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠魁 2024-10-16 13:10:54

像这样的东西应该有效:

@reqs = []    
@user.accommodations.all.each do |a|
    @reqs << a.notification.request
end

假设这是正确的:

class User
    has_many :accommodations
end

class Accommodation
    belongs_to :user
    has_many :notifications
end

class Notification
    belongs_to :accomodation
    belongs_to :request
end

class Request
    has_many :notifications
end

使用 has_many :through 不适用于多个模型,如下所示:Ruby-on-Rails: 多个 has_many :through 可能吗?

但是你可以在你的用户模型中做这样的事情:

class User
    has_many :accommodations
    has_many :notifications,
             :through => :accommodations

    def requests 
        self.notifications.all.collect{|n| n.request }
    end
end

Something like this should work:

@reqs = []    
@user.accommodations.all.each do |a|
    @reqs << a.notification.request
end

Assuming this is correct:

class User
    has_many :accommodations
end

class Accommodation
    belongs_to :user
    has_many :notifications
end

class Notification
    belongs_to :accomodation
    belongs_to :request
end

class Request
    has_many :notifications
end

Using has_many :through will not work for multiple models, as seen here: Ruby-on-Rails: Multiple has_many :through possible?

But you can do something like this in your user model:

class User
    has_many :accommodations
    has_many :notifications,
             :through => :accommodations

    def requests 
        self.notifications.all.collect{|n| n.request }
    end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文