On Friday, 25 April 2014 03:59:56 UTC-4, [email protected] wrote: > > On Thursday, April 24, 2014 5:35:08 PM UTC+2, Colin Law wrote: >> >> For those of us with less knowledge of how the internals of rails >> works could you provide a simple example of what you are attempting to >> achieve with dynamic class name in a association? >> > > I can't speak for the OP, but in my case I needed something like: > > class Report < ActiveRecord::Base > has_many :reports_subjects > has_many :subjects, through: :reports_subjects, class_name: ->(report) { > report.subjects_type } > > def subjects_type > # divine required subject model class somehow > end > end > > Of course, that won't fly since the class_name is evaluated purely as a > string deep down in the reflection and at that point, all knowledge of the > specific instances involved in an association has been discarded. >
This doesn't make any sense to me - if I request a ReportsSubject object from the database directly (via `find`, for instance), what do I get if I ask for its subject? What would the reports_subjects table even *store*? A bare `subject_id` would be insufficient since without a class_name it's unclear what table that ID refers to. And that's not even considering what should happen when this sort of code runs: Report.joins(:subjects).where(name: 'hey wait WHAT TABLE IS THIS EVEN QUERYING') --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/58d4e33d-1ad5-4afe-9db7-9be0c4cd1cbc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

