Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:16:25+00:00 2026-06-18T02:16:25+00:00

I am using a has many through association as follows: class Rating < ActiveRecord::Base

  • 0

I am using a has many through association as follows:

class Rating < ActiveRecord::Base  
  has_many :recommendation_ratings, :dependent => :destroy
  has_many :recommendations, :through => :recommendation_ratings
end

class Recommendation < ActiveRecord::Base  
  has_many :recommendation_ratings, :dependent => :destroy
  has_many :ratings, :through => :recommendation_ratings
end

class RecommendationRating < ActiveRecord::Base  
  validates :recommendation_id, presence: true
  validates :rating_id, presence: true

  belongs_to :recommendation
  belongs_to :rating
end

I am currently creating my associations in my controller as follows:

   rr   = RecommendationRating.find_by_recommendation_id(recommendation_id)
   rr ||= RecommendationRating.new(recommendation_id: recommendation_id)

   rr.rating_id = rating_id
   rr.save

Alternatively, I could assign ratings to recommendations like so:

   @recommendation = Recommendation.find(recommendation_id)
   @rating = Rating.find(rating_id)
   @recommendation.ratings << @rating

Which is the correct method to associate these records, and is there a downside to doing it the first way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:16:26+00:00Added an answer on June 18, 2026 at 2:16 am

    That depends on what you need.

    The first one updates a recommendation_rating record if it already exists and enables you to run callbacks and validation after calling save

    The second one creates a rating everytime you call << and it creates the record directly on the database so no validation and callback is triggered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Rails 2.3.5. Class User < ActiveRecord::Base has_many :phones end class Phone
I'm having problems adding to a Has Many Through association using user_ids . My
I'm using has_many :through to specify a many-to-many association between two entities and I
I have a has many through association. Firms have many Users through Follows. I
I am using a has_many through association and having trouble getting the before_destroy call
I'm using a :has_many, :through association to link two models, User and Place It
Using has_many => through association. Here is what i have. :planning model has_many :acttypes
So I have the following models in my program: class User < ActiveRecord::Base has_many
I have some model classes like this: class Organisation < ActiveRecord::Base has_many :dongles has_many
I'm learning Ruby on Rails. I'm using some associations (has_many :through, ...) But i'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.