Ruby on rails Rails 4 activerecord过滤器
我正在编写一个rails 1.x应用程序,我在很多年前编写了这个应用程序,但在使用rails 4过滤activerecord时遇到了问题 型号: 釉料:Ruby on rails Rails 4 activerecord过滤器,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我正在编写一个rails 1.x应用程序,我在很多年前编写了这个应用程序,但在使用rails 4过滤activerecord时遇到了问题 型号: 釉料: has_many :ingredients, dependent: :destroy has_many :materials, through: :ingredients 成分: belongs_to :glaze belongs_to :material 材料: has_many :ingredients has_many :gla
has_many :ingredients, dependent: :destroy
has_many :materials, through: :ingredients
成分:
belongs_to :glaze
belongs_to :material
材料:
has_many :ingredients
has_many :glazes, through: :ingredients
每个材质都有一个布尔复选框,用于指示它是否为着色剂。我想为每个釉料记录过滤非着色剂的材料
在我的旧rail 1.x应用程序中,我使用了以下代码:
@batchingredients = @glaze.ingredients.find(:all, :order => 'amt DESC', :include => :material, :conditions => ['materials.colorant = ?', false])
我不能让它通过非着色剂材料。我也不确定这是否是我应该为之创建范围的任务(我不是程序员,我不认为范围是上次使用Rails时的范围)。最好在您的成分模型上创建范围:
scope :without_colorant, -> { joins(:material).where(materials: { colorant: false }) }
那么您的控制器将如下所示:
@batchingredients = @glaze.ingredients.without_colorant.order(amt: :desc)
想必,找到非着色剂的材料在其他地方可能有用。因此,可能在
材料上有一个作用域
作用域:not_colorant,{where(colorant:false)}
,然后可以在任何材料上使用它,并且可以在你的作用域中使用作用域:不含染色剂,->{joins(:Material)。merge(Material.not_colorant)}