【问题标题】:Rails polymorphic association not available in modelsRails 多态关联在模型中不可用
【发布时间】:2016-03-17 00:12:38
【问题描述】:

我是 Rails 新手,所以请耐心等待,我已经搜索了一整天。如果这是一个初学者问题,请道歉:)

我有一个定义了多态关联的模型:

class SocialLink < ActiveRecord::Base
  belongs_to :social, polymorphic: true
end

还有两个模型应该具有这种关联之一

class Staff < ActiveRecord::Base
  validates :name, presence: true
  belongs_to :establishment
  has_one :image, as: :imageable
  has_one :social_link, as: :social
end


class Establishment < ActiveRecord::Base
  validates :name, presence: true
  has_one :location
  has_one :social_link, as: :social
  has_many :staff

  accepts_nested_attributes_for :location
  accepts_nested_attributes_for :staff
end

编辑:社交链接的初始表创建迁移

class CreateSocialLinks < ActiveRecord::Migration
  def change
    create_table :social_links do |t|
      t.string :facebook
      t.string :twitter
      t.string :yelp
      t.string :google_plus
      t.string :youtube
      t.string :instagram
      t.string :linkedin

      t.timestamps null: false
    end
  end
end

为此创建的迁移是这样创建的(编辑:请注意迁移时该表存在)

class AddSocialLinkReferenceToEstablishmentAndStaff < ActiveRecord::Migration
  def change
    add_reference :social_links, :social_links, polymorphic: true, index: true
  end
end


class UpdateSocialLinkReference < ActiveRecord::Migration
  def change
    remove_reference :social_links, :social_links
    add_reference :social_links, :social, polymorphic: true, index: true
  end
end

编辑:经过上述迁移,social_id 和 social_type 可用

#<SocialLink id: nil, facebook: nil, twitter: nil, yelp: nil, google_plus: nil, youtube: nil, instagram: nil, linkedin: nil, created_at: nil, updated_at: nil, social_id: nil, social_type: nil> 

但是,由于某种原因,该关联在这两种型号中均不可用。我无法看到我做错了什么,看起来我已经将它设置为与正在工作的图像模型上的另一个多态关联相同

这个正在工作

class Image < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
end

感谢或帮助!

【问题讨论】:

  • 你到底有什么问题?

标签: ruby-on-rails ruby-on-rails-4 polymorphic-associations


【解决方案1】:

根据rails guide on polymorphic associations,您的迁移应该看起来更像这样:

class CreateSocialLinks < ActiveRecord::Migration
  def change
    create_table :social_links do |t|
      t.string :name
      t.references :social, polymorphic: true, index: true
      t.timestamps null: false
    end
  end
end

否则表格会丢失

【讨论】:

  • 您好@Stefan,该表在迁移时已经存在。您可以看到我分享的迁移后的 social_id 和 social_type 列:#&lt;SocialLink id: nil, facebook: nil, twitter: nil, yelp: nil, google_plus: nil, youtube: nil, instagram: nil, linkedin: nil, created_at: nil, updated_at: nil, social_id: nil, social_type: nil&gt;
  • 我试图重现这一点,但您上面的迁移创建了以下 # 它应该是 social_id 和 social_type . (但否则你是对的。对不起。)
  • 嘿 Stefan,添加了我使用的表创建代码的编辑。首先创建表,然后运行我列出的迁移以添加引用。希望这会有所帮助
  • 我的意思是 add_reference :social_links, :social_links, polymorphic: true, index: true 应该是 add_reference :social_links, :social, polymorphic: true, index: true
  • 抱歉 Stefan,我错过了迁移。起初它是 social_links 多态的,但后来我读到多态名称不应该与表名相同,所以我将其更改为只是社交。很抱歉造成混乱
【解决方案2】:

问题是我试图使用多态名称而不是仅使用模型名称来访问关联。而不是做staff.social,我应该使用staff.social_link。我对这部分感到困惑。

抱歉给您带来了困扰

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 2018-10-23
    相关资源
    最近更新 更多