Use a scope by default on a Rails has_many relationship

前端 未结 3 1009
难免孤独
难免孤独 2021-01-30 05:13

Let\'s say I have the following classes

class SolarSystem < ActiveRecord::Base
  has_many :planets
end

class Planet < ActiveRecord::Base
  scope :life_supp         


        
3条回答
  •  無奈伤痛
    2021-01-30 05:48

    i just had a deep dive into ActiveRecord and it does not look like if this can be achieved with the current implementation of has_many. you can pass a block to :conditions but this is limited to returning a hash of conditions, not any kind of arel stuff.

    a really simple and transparent way to achieve what you want (what i think you are trying to do) is to apply the scope at runtime:

      # foo.rb
      def bars
        super.baz
      end
    

    this is far from what you are asking for, but it might just work ;)

提交回复
热议问题