小编Joh*_*ohn的帖子

Rails InvalidForeignKey

我正在为我的网站开发作品集,因此我决定为每个作品集增加技能。

class PortfolioSkill < ApplicationRecord
  belongs_to :portfolio
  belongs_to :skill
end

class Portfolio < ApplicationRecord

  has_many :portfolio_skills
  has_many :skills, through: :portfolio_skills


  def all_tags=(names)
    self.skills = names.split(",").map do |name|
      Skill.where(name: name.strip).first_or_create!
  end
end

 def all_tags
  self.skills.map(&:name).join(", ")
 end

 def remove_skill_tags
    PortfolioSkill.where(portfolio_id: id).destroy_all
 end


end



 create_table "portfolio_skills", force: :cascade do |t|
     t.integer "portfolio_id"
     t.integer "skill_id"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.index ["portfolio_id"], name:     "index_portfolio_skills_on_portfolio_id"
     t.index ["skill_id"], name: "index_portfolio_skills_on_skill_id"
  end

create_table "portfolios", force: :cascade do |t|
    t.string "name"
    t.string "client"
    t.date …
Run Code Online (Sandbox Code Playgroud)

sqlite ruby-on-rails rails-activerecord

3
推荐指数
1
解决办法
2915
查看次数

标签 统计

rails-activerecord ×1

ruby-on-rails ×1

sqlite ×1