Invalid Source Reflection MACro :Has_Many :Through

Invalid source reflection macro :has_many :through

The error you get is about source_reflection is an invalid association, because source for has_many through must be belongs_to, has_one or has_many without through option. So you can't use :last_actual_financings as a source.

Rails - Invalid source reflection for :has_many = :through

Rails won't allow you chain nested has_many :through associations like that.

There's a similar question here: Ruby-on-Rails: Multiple has_many :through possible?

rails association eager loading with has_many :through

Although there are some plugins that extend the :through relationship functionality, it's not generally possible to use ActiveRecord to navigate through multiple layers at the same time. The limit is generally one :through, no more.

Rails or SQL to get through a few model associations

If I understand correctly, this is the SQL you want (for example):

SELECT c.*
FROM courses c
LEFT JOIN streams s ON s.course_id = c.id
LEFT JOIN enrollments e ON e.stream_id = s.id
LEFT JOIN timetables t ON e.timetable_id = t.id
WHERE t.id = 5

To achieve this in rails, have a look at

Course.joins(:streams => {:enrollments => :timetables}).to_sql

This shows you the sql it generates. You can then apply other conditions like so

Course.joins(:streams => {:enrollments => :timetables}).where(some condition on timetables)

Rails has_many through singular association

has_many :through doesn't work with has_one relations on the join model; see the following Rails tickets for details:

  • https://rails.lighthouseapp.com/projects/8994/tickets/1149
  • https://rails.lighthouseapp.com/projects/8994/tickets/2719-has_many-through-should-work-if-the-join-model-uses-has_one-relationship

If possible, the best solution is probably to modify your relations so that a Booking belongs_to a Rating rather than has_one Rating.

:has_many :through associations two levels deep

Ryan is right, this is supported from Rails 3.1. Extracted from the release notes:

Associations with a :through option can now use any association as the through or source association, including other associations which have a :through option and has_and_belongs_to_many associations.

Src: http://guides.rubyonrails.org/3_1_release_notes.html

How do I specify associations in Rails that pass through several models

The source for a has_many association must be a belongs_to, has_one, or has_many association without a :through option (thanks to this answer for info).

There is a plugin that some people have had success with, but in my case it didn't seem to work correctly with my taggable polymorphic association.

At the moment, my solution is to pass a finder_sql option to has_many:

class Store < ActiveRecord::Base
has_many :items
has_many :tags, :finder_sql =>
'SELECT tags.* from tags
INNER JOIN taggings on tags.id = taggings.tag_id
INNER JOIN items on items.id = taggings.taggable_id AND taggings.taggable_type = "Item"
WHERE items.store_id = #{id}'

end


Related Topics



Leave a reply



Submit