Ruby on rails 如何获取Rails中关联表的某一列的值?

Ruby on rails 如何获取Rails中关联表的某一列的值?,ruby-on-rails,ruby,ruby-on-rails-3,database-design,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Database Design,Activerecord,如何通过记录模型获得产品重量?据我所知,可以得到所有产品的某些记录,但我无法找到的方式获得某些产品的重量 class User < ActiveRecord::Base has_many :eatings end class Eating < ActiveRecord::Base belongs_to :user has_many :records end class Record < ActiveRecord::Base belongs_to :eatin

如何通过记录模型获得产品重量?据我所知,可以得到所有产品的某些记录,但我无法找到的方式获得某些产品的重量

class User < ActiveRecord::Base
  has_many :eatings
end

class Eating < ActiveRecord::Base
  belongs_to :user
  has_many :records
end

class Record < ActiveRecord::Base
  belongs_to :eating
end

class Product < ActiveRecord::Base
end

class WeightedProduct < ActiveRecord::Base
end

class用户

记录和产品型号与WeightedProduct之间应该有什么关系,这样用户就可以通过一行用户获得特定产品的重量。first.eatings.first.records.first.products.first.weight?

看起来您在追求以下目标:

class Record < ActiveRecord::Base
  belongs_to :eating
  has_many :weighted_products
end

class Product < ActiveRecord::Base
  has_many :weighted_products
end

class WeightedProduct < ActiveRecord::Base
  belongs_to :record
  belongs_to :product
end
class记录
然后
User.first.eatings.first.records.first.weighted\u products.first.weight


我认为这应该有效,但还没有测试。

似乎每个产品都有一个加权产品,那么在这种情况下,您应该添加

class Product < ActiveRecord::Base
 has_one :weighted_product
end


class WeightedProduct < ActiveRecord::Base
 belongs_to :product
end
类产品

class记录
 class Record < ActiveRecord::Base
  belongs_to :eating
  has_many :products 
 end