Rails 4访问连接表属性

时间:2014-08-11 02:14:59

标签: sql ruby-on-rails ruby-on-rails-4

我为has_many throughIngredient通过Meal连接的食谱应用设置了MealIngredient联接表设置。在MealIngredient内,我有meal_idingredient_idamount。我的问题是:如何访问amount列?

在我的食谱视图中,我遍历了各个成分:

@meal.ingredients.each do |i|

我可以访问该成分的属性,但不能访问MealIngredient联接记录中的金额。

我尝试在执行includes的查询中使用@meal.ingredients.includes(:meal_ingredients),但我不确定如何在上述循环中访问amount。当我使用i.inspect时,我根本看不到对meal_ingredients表的任何引用。

是否有某种方法可以使用i.amount访问该循环中的变量?

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:25)

在这种情况下,您应该遍历meal_ingredients关联。您应该急切加载ingredients关联以减少数据库查询。

@meal.meal_ingredients.includes(:ingredient).each do |meal_ingredient|
  puts meal_ingredient.amount
  puts meal_ingredient.ingredient.name
end

<强>更新

此次更新是在Rich Peck的回答之后发生的,但我认为实现目标的方式更简单。

@meal.ingredients.select('ingredients.*, meal_ingredients.amount').each do |ingredient|
  puts ingredient.amount
  puts ingredient.name
end

答案 1 :(得分:24)

啊,这个古老的how do I access my extra join table attributes问题。在我们想出一个解决方案之前,为MONTHS挣扎了

-

ActiveRecord Association Extensions

您遇到的问题是Rails只会使用连接表中的foreign_keys来加载您需要的关联数据。除非您实际直接加载连接模型,否则它不会让您能够访问连接属性

一些觅食导致我们ActiveRecord Association Extensions - 一种访问不同ActiveRecord关联之间的中间数据的方法(使用名为proxy_association的集合)。这将允许您从连接模型访问额外的属性,将它们附加到您的&#34;原始&#34;模型:

#app/models/ingredient.rb
class Ingredient < ActiveRecord::Base
   attr_accessor :amount #-> need a setter/getter
end

#app/models/meal.rb
class Meal < ActiveRecord::Base
   has_many :meal_ingredients
   has_many :ingredients, through: :meal_ingredients, extend: IngredientAmount
end

#app/models/concerns/ingerdient_amount.rb
module IngredientAmount

    #Load
    def load
        amounts.each do |amount|
            proxy_association.target << amount
        end
    end

    #Private
    private

    #Amounts
    def amounts
        return_array = []
        through_collection.each_with_index do |through,i|
            associate = through.send(reflection_name)
            associate.assign_attributes({amount: items[i]}) if items[i].present?
            return_array.concat Array.new(1).fill( associate )
        end
        return_array
    end

    #######################
    #      Variables      #
    #######################

    #Association
    def reflection_name
        proxy_association.source_reflection.name
    end

    #Foreign Key
    def through_source_key
        proxy_association.reflection.source_reflection.foreign_key
    end

    #Primary Key
    def through_primary_key
        proxy_association.reflection.through_reflection.active_record_primary_key
    end

    #Through Name
    def through_name
        proxy_association.reflection.through_reflection.name
    end

    #Through
    def through_collection
        proxy_association.owner.send through_name
    end

    #Captions
    def items
        through_collection.map(&:amount)
    end

    #Target
    def target_collection
        #load_target
        proxy_association.target
    end

end

这应该会立即将amount属性附加到您的ingredient对象,让您执行以下操作:

@meal = Meal.find 1
@meal.ingredients.each do |ingredient|
   ingredient.amount
end