Say I have the following classes
class SolarSystem < ActiveRecord::Base has_many :planets end class Planet < ActiveRecord::Base scope :life_supporting, where('distance_from_sun > ?', 5).order('diameter ASC') end
Planet has the scope of life_supporting and SolarSystem has_many :planets . I would like to define my has_many relationship, so when I set the solar_system for all related planets , the scope of life_supporting automatically applied. Essentially, I would like solar_system.planets == solar_system.planets.life_supporting .
Requirements
I want to not change scope :life_supporting in Planet to
default_scope where('distance_from_sun > ?', 5).order('diameter ASC')
I would also like to prevent duplication without adding to SolarSystem
has_many :planets, :conditions => ['distance_from_sun > ?', 5], :order => 'diameter ASC'
goal
I would like to have something like
has_many :planets, :with_scope => :life_supporting
Edit: work around
As @phoet said, it may not be possible to reach the default area using ActiveRecord. However, I found two potential problems. Both prevent duplication. The first, although lengthy, retains obvious readability and transparency, and the second is an auxiliary method, the output of which is obvious.
class SolarSystem < ActiveRecord::Base has_many :planets, :conditions => Planet.life_supporting.where_values, :order => Planet.life_supporting.order_values end class Planet < ActiveRecord::Base scope :life_supporting, where('distance_from_sun > ?', 5).order('diameter ASC') end
Another solution that is much cleaner is to simply add the following method to SolarSystem
def life_supporting_planets planets.life_supporting end
and use solar_system.life_supporting_planets wherever you would use solar_system.planets .
None of the answers answer the question, so I just put them here as work if someone else comes across this situation.
activerecord ruby-on-rails-3 has-many
Aaron Jul 24 '12 at 17:53 2012-07-24 17:53
source share