如何调用多态关联的位置?

时间:2015-04-16 13:15:06

标签: ruby-on-rails

我这样的三个模型具有多态关联:

主要型号:

# == Schema Information
#
# Table name: packages
#
#  id         :integer          not null, primary key
#  name       :string
#  price      :money
#  plan_id    :integer
#  plan_type  :string
#  active     :boolean          default(FALSE)
#  order      :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Package < ActiveRecord::Base
    belongs_to :plan, polymorphic: true
    monetize :price
end

一个孩子:

# == Schema Information
#
# Table name: timepackages
#
#  id         :integer          not null, primary key
#  months     :integer
#  downloads  :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Timepackage < ActiveRecord::Base
    has_one :package, as: :plan, dependent: :destroy
end

我需要获取Timepackages值为months的所有{{1}}。我该怎么做?

1 个答案:

答案 0 :(得分:1)

Timepackage.where(:months => 1)

我不知道您问题标题中提到的多态关系是如何进入实际问题中列出的要求的。