Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Filtering a model based on two other models.

rails ruby model relation controller views

  • Please log in to reply
1 reply to this topic

#1 svajone

svajone

    Passenger

  • Members
  • 3 posts

Posted 07 August 2014 - 05:24 AM

So I have these models:

class User < ActiveRecord::Base
  has_many :ratings
end

class Rating < ActiveRecord::Base
  has_many :restaurant_ratings
  has_many :restaurant, through: :restaurant_ratings
  has_many :belongs
end

class Restaurant < ActiveRecord::Base
  has_many :restaurant_ratings
  has_many :ratings, through: :restaurant_ratings
end

class RestaurantRating < ActiveRecord::Base
  belongs_to :restaurant
  belongs_to :rating
end 

Currently, when I want to view a restaurant on the controller's show method it works just fine. But right now, I am trying to figure out a way to filter a specific rating based on the current restaurant that is being viewed or shown as well as the current user who is logged in.

 

How would I best tackle this?



#2 sidk

sidk

    Dispatcher

  • Members
  • 55 posts

Posted 24 May 2016 - 09:10 PM

Hey there, 

 

This is very late to the party but if anyone stumbles on this, you might find the following article I wrote useful:

 

http://ducktypelabs....y-associations/

 

In it, I go over a few options that are available when you want to filter your associations.


Join my free, 4-part course on Managing Technical Debt in your Rails app at http://email-course.ducktypelabs.com






Also tagged with one or more of these keywords: rails, ruby, model, relation, controller, views

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users