Convert Rails 4 has_many from a condition with proc, where

I have the following has_many work with proc to capture a parameter for context:

has_many :subclass_point_analytics, :conditions => proc {"assessment_id = #{self.send(:assessment_id)}" }, :foreign_key => 'gid', :dependent => :destroy 

I use Rails 4, and he (rightfully) complains about using: conditions. After 30 minutes and many attempts, I can’t understand how to convert: conditions to the format β†’ {where ...}. I would appreciate someone who knows the proc syntax to help me understand correctly.

+6
source share
2 answers

Just do the following:

 has_many :subclass_point_analytics, -> (object) { where("assessment_id = ?", object.assessment_id) }, :foreign_key => 'gid', :dependent => :destroy 
An object

- your actual copy. Also note: the called must be the first (conditions usually end)

+15
source

I would start with something like this:

 has_many :subclass_point_analytics, -> { where("assessment_id = #{self.send(:assessment_id)}") }, :foreign_key => 'gid', :dependent => :destroy 
0
source

Source: https://habr.com/ru/post/955612/


All Articles