如何在rails 3中编写has_many_polymorphs

时间:2014-11-02 12:56:34

标签: ruby-on-rails ruby ruby-on-rails-3

如何在不使用has_many_polymorphs插件的情况下在rails 3中编写以下代码?

has_many_polymorphs :listings, :from => [:my_properties, :friend_properties, :public_properties, :private_properties, :job_listings, :truck_listings, :open_listings, :sale_listings], :through => :group_listings

1 个答案:

答案 0 :(得分:0)

此视频帮助我分配#154 Polymorphic Association

在您的Polymorphic表中的键中,您需要 ID 您想要链接到的记录以及要链接到的表的名称。 我通常这样做:

<强>移植

  create_table "cards", force: true do |t|
    t.string   '....',      default: ""
    t.integer  "accountable_id"
    t.string   "accountable_type",    default: ""
  end

卡片型号

  belongs_to :accountable, :polymorphic => true

其他模特

has_many :cards, :as => :accountable

我希望这有助于