Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RailsAdmin & letsrate Error (Rails4) #38

Open
initabas opened this issue Jul 15, 2013 · 2 comments
Open

RailsAdmin & letsrate Error (Rails4) #38

initabas opened this issue Jul 15, 2013 · 2 comments

Comments

@initabas
Copy link

After installation of letsrate
when I'm trying to edit any of my model in RailsAdmin I get

ActiveRecord::Reflection::ThroughReflection#foreign_key delegated to source_reflection.foreign_key, but source_reflection is nil

but in general letsrate works!

@kiryl-t
Copy link

kiryl-t commented Sep 1, 2013

These links helped me to solve the problem (actually I've got the error on dashboard load)
http://stackoverflow.com/questions/16677703/source-reflection-errors-with-has-many-through
https://github.com/muratguzel/letsrate/blob/master/lib/letsrate/model.rb

Instead of writing

letsrate_rateable :content

in my model, I've copied "letsrate_rateable" method code and specified the source_type for has_many *_raters association.
So I've got something like this

has_many :rates_without_dimension, :as => :rateable, :class_name => "Rate", :dependent => :destroy, :conditions => {:dimension => nil}
has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater

has_one :rate_average_without_dimension, :as => :cacheable, :class_name => "RatingCache",
        :dependent => :destroy, :conditions => {:dimension => nil}

has_many :content_rates, :dependent => :destroy,
         :conditions => {:dimension => :content.to_s},
         :class_name => "Rate",
         :as => :rateable

has_many :content_raters, :through => :content_rates, :source => :rater, **:source_type => "User"**

has_one :content_average, :as => :cacheable, :class_name => "RatingCache",
        :dependent => :destroy, :conditions => {:dimension => :content.to_s}

And it works.

@yawo
Copy link

yawo commented Jan 12, 2014

Tx a lot

Your solution works fine.

There was indeed that problem.
Another one was a to_sym missing in letsrate code on

has_many "#{dimension}_raters".to_sym, :through => "#{dimension}_rates", :source => :rater

it should be

has_many "#{dimension}_raters".to_sym, :through => "#{dimension}_rates".to_sym, :source => :rater

As I had many models, I put my code in a concern under app/models/concerns.
app/models/concerns/myletsrate.rb :

require 'active_support/concern'
module Concerns::Myletsrate  extend  ActiveSupport::Concern
  module ClassMethods
    def my_letsrate_rateable(*dimensions)

      has_many :rates_without_dimension, -> { where dimension: nil}, :as => :rateable, :class_name => "Rate", :dependent => :destroy
      has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater

      has_one :rate_average_without_dimension, -> { where dimension: nil}, :as => :cacheable,
              :class_name => "RatingCache", :dependent => :destroy


      dimensions.each do |dimension|
        has_many "#{dimension}_rates".to_sym, -> {where dimension: dimension.to_s},
                 :dependent => :destroy,
                 :class_name => "Rate",
                 :as => :rateable

        has_many "#{dimension}_raters".to_sym, :through => "#{dimension}_rates".to_sym, :source => :rater, source_type: "User"

        has_one "#{dimension}_average".to_sym, -> { where dimension: dimension.to_s },
                :as => :cacheable, :class_name => "RatingCache",
                :dependent => :destroy
      end

    end
  end
end

Then add this to config/initializers/rails_admin.rb:

config.excluded_models = Dir.glob(Rails.root.join('app/models/concerns/**.rb')).map {|p| 'Concerns::' + File.basename(p, '.rb').camelize }

Finally in each model rateable I add this

  include Concerns::Myletsrate
  my_letsrate_rateable "feelings"

It works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants