简体   繁体   English

多态关联,创建记录

[英]Polymorphic association, creating a record

I'm trying to seed a GroupItem record but I'm not providing the correct data into the create method. 我正在尝试为GroupItem记录添加种子,但是没有将正确的数据提供给create方法。

Here's what I tried in the rails console: 这是我在Rails控制台中尝试过的方法:

irb(main):055:0> group = Group.first
  Group Load (0.2ms)  SELECT  "groups".* FROM "groups" ORDER BY "groups"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Group id: 1, groupable_type: "Legislator", name: "Cool Peeps", created_at: "2017-02-23 01:13:21", updated_at: "2017-02-23 01:13:21">

irb(main):056:0> person = Role.first
  Role Load (0.2ms)  SELECT  "roles".* FROM "roles" ORDER BY "roles"."role_id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Role role_id: 100, caucus: nil, congress_numbers: "[106]", current: false, description: "Representative for Wisconsin's 2nd congressional d...", district: 2, enddate: "2001-01-03", address: nil, contact_form: nil, fax: nil, office: nil, rss_url: nil, how: nil, leadership_title: nil, party: "Democrat", person_id: 400013, phone: nil, role_type: "representative", role_type_label: "Representative", senator_class: nil, senator_rank: nil, startdate: "1999-01-06", state: "WI", title: "Rep.", title_long: "Representative", website: "", created_at: "2017-02-23 01:11:08", updated_at: "2017-02-23 01:11:08">

irb(main):059:0> GroupItem.create(group_id: group.id, groupable_type: group.groupable_type, groupable_id: person.id)
   (0.1ms)  begin transaction
  Legislator Load (0.1ms)  SELECT  "legislators".* FROM "legislators" WHERE "legislators"."id" = ? LIMIT ?  [["id", 100], ["LIMIT", 1]]
  Group Load (0.1ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.1ms)  rollback transaction
=> #<GroupItem id: nil, group_id: 1, groupable_type: "Legislator", groupable_id: 100, created_at: nil, updated_at: nil>

here is the set-up: 这是设置:

class Group < ApplicationRecord
    has_many :group_items
end

class GroupItem < ApplicationRecord
    belongs_to :groupable, polymorphic: true
    belongs_to :group 
end

class Role < ApplicationRecord
    belongs_to :person, foreign_key: :person_id
    has_many :group_items, as: :groupable
    #....
end

# migrations...


class CreateGroups < ActiveRecord::Migration[5.0]
  def change
    create_table :groups do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateGroupItems < ActiveRecord::Migration[5.0]
  def change
    create_table :group_items do |t|
      t.integer :group_id
      t.references :groupable, polymorphic: true, index: true

      t.timestamps
    end
  end
end

class CreateRoles < ActiveRecord::Migration[5.0]
  def change
    create_table :roles, :id => false do |t|
         t.primary_key :role_id
         #...

         t.timestamps

    end
  end
end

It must be a problem with the associations, this is my first venture into polymorphic so I probably messed up something there. 关联一定是有问题的,这是我第一次尝试多态,所以我可能在这里搞砸了。

I fixed this issue by doing this: 我通过执行以下操作解决了此问题:

   new_item = GroupItem.new(group_id: group.id)
   new_item.update_attribute(:groupable, person)

irb(main):046:0> new_item = GroupItem.new(group_id: group.id)
=> #<GroupItem id: nil, group_id: 1, groupable_type: nil, groupable_id: nil, created_at: nil, updated_at: nil>

irb(main):047:0> new_item.update_attribute(:groupable, person)
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "group_items" ("group_id", "groupable_type", "groupable_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["group_id", 1], ["groupable_type", "Role"], ["groupable_id", 29], ["created_at", 2017-02-23 03:14:33 UTC], ["updated_at", 2017-02-23 03:14:33 UTC]]
   (1.6ms)  commit transaction
=> true

irb(main):049:0> new_item
=> #<GroupItem id: 3, group_id: 1, groupable_type: "Role", groupable_id: 29, created_at: "2017-02-23 03:14:33", updated_at: "2017-02-23 03:14:33">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM