Ruby on Rails
Friday, April 25, 2014
On Thursday, April 24, 2014 5:35:08 PM UTC+2, Colin Law wrote:
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.
-- 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.
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1179691a-062d-4803-a5ac-daed66de7940%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment