0

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
Eric R.
  • 933
  • 1
  • 9
  • 19

1 Answers1

1

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
Community
  • 1
  • 1
zsalzbank
  • 9,685
  • 1
  • 26
  • 39
  • Thanks codethis. I thought about implementing something along those lines. However, I was told that there is a more "proper" way to do this... Something like has_many :notifications, :through => :accommodations? – Eric R. Dec 28 '10 at 04:56
  • For some reason I'm getting an array returned that looks like this: # # – Eric R. Dec 28 '10 at 19:31
  • this works for me - can you post your code above? is it exactly the same? – zsalzbank Dec 28 '10 at 21:46
  • I edited my question to include the code. The original error I got from your code was that 'request.notification_id' didn't exist in my table. – Eric R. Dec 28 '10 at 22:33
  • Yes, that works as well. Now the issue is when I use @requests in my view code, I get errors like this: undefined method `firstname' for nil:NilClass. Why is it saying it's a NilClass? It doesn't seem to be accessing, or storing, the data properly in the array. – Eric R. Dec 28 '10 at 23:05
  • what is `@requests.length`? It seems like you are not returning anything – zsalzbank Dec 28 '10 at 23:07
  • I just got it working! Apparently I deleted a notification which got the join table out of sync with everything. Thanks a lot CodeThis, your suggestions were a huge help. – Eric R. Dec 29 '10 at 23:50