Search code examples
ruby-on-railsrubyruby-on-rails-4deviserating

Devise User Review/Rating System in Rails 4


I'm trying to create a review system on users in rails. I want one user to be able to rate another user on their profile page in devise. I've tried a few different methods but I am fairly new to rails and haven't been able to accomplish this.

Right now I have default devise views but no user profile page. I'd like users to review a another user on 5 or so different issues.

Any help would be much appreciated!


Solution

  • In order to do that, you can use the association called has_many through association : http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

    Your models should look like that "

    class User < ActiveRecord::Base
      has_many :rates
      has_many :rated_users, through: :rates, class_name: "User", foreign_key: :rated_user_id # The users this user has rated
      has_many :rated_by_users, through: :rates, class_name: "User", foreign_key: :rating_user_id # The users that have rated this client
    end
    
    class Rates < ActiveRecord::Base
      belongs_to :rating_user, class_name: "User"
      belongs_to :rated_user, class_name: "User"
    end
    

    And your migrations :

    class createRates < ActiveRecord::Migration
    
    
      def change
        create_table :changes do |t|
          t.belongs_to :rated_user
          t.belongs_to :rating_user
          t.integer :value
          t.timestamps
        end
      end
    end