首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails中的“标记”应该使用什么关联?

Rails中的“标记”应该使用什么关联?
EN

Stack Overflow用户
提问于 2015-01-18 22:52:14
回答 1查看 68关注 0票数 0

AngelList.co有一个API,返回关于其公司、工作和人员的数据。

每个公司、工作和个人记录都可以有几个“标签”,上面有“技能”、“位置”、“角色”、“市场”等信息。

这些“标签”将如何在Rails中建模?

公司的JSON示例:

代码语言:javascript
复制
{
  "id": 6702,
  "name": "AngelList",
  "angellist_url": "http://localhost:3000/angellist",
  "markets": [
    {
      "id": 448,
      "tag_type": "MarketTag",
      "name": "startups",
      "display_name": "Startups",
      "angellist_url": "http://angel.co/startups-1"
    },
    {
      "id": 856,
      "tag_type": "MarketTag",
      "name": "venture capital",
      "display_name": "Venture Capital",
      "angellist_url": "http://angel.co/venture-capital"
    }
  ],
  "locations": [
    {
      "id": 1692,
      "tag_type": "LocationTag",
      "name": "san francisco",
      "display_name": "San Francisco",
      "angellist_url": "http://angel.co/san-francisco"
    }
  ],
  "status": {
    "message": "You're insane if you don't follow the AngelList Twitter...",
    "created_at": "2011-08-07T00:56:25Z"
  },
  "screenshots": [
    {
      "thumb": "https://s3.amazonaws.com/.../009cff275fb96709c915c4d4-thumb.jpg",
      "original": "https://s3.amazonaws.com/.../009cff275fb96709c915c4d4-original.jpg"
    }
  ]
}

作业的示例JSON:

代码语言:javascript
复制
{
  "id": 97,
  "title": "Venture Hacker",
  "created_at": "2011-12-05T21:05:43Z",
  "updated_at": "2012-02-13T03:40:17Z",
  "equity_cliff": 1.0,
  "equity_min": 0.25,
  "equity_max": 0.25,
  "equity_vest": 4.0,
  "salary_min": 100000,
  "salary_max": 100000,
  "job_type": "full-time",
  "angellist_url": "http://angel.co/angellist/jobs/97",
  "startup": {
    "id": 6702,
    "hidden": false,
    "name": "AngelList",
    "angellist_url": "http://angel.co/angellist",
    "logo_url": "https://s3.amazonaws.com/photos.angel.co/startups/i/6702-...",
    "thumb_url": "https://s3.amazonaws.com/photos.angel.co/startups/i/6702-...",
    "product_desc": "AngelList is an online community that helps startups...",
    "high_concept": "Platform for startups",
    "follower_count": 876,
    "company_url": "http://angel.co"
  },
  "tags": [
    {
      "id": 14766,
      "tag_type": "SkillTag",
      "name": "software engineering",
      "display_name": "Software Engineering",
      "angellist_url": "http://angel.co/software-engineering"
    },
    {
      "id": 1692,
      "tag_type": "LocationTag",
      "name": "san francisco",
      "display_name": "San Francisco",
      "angellist_url": "http://angel.co/san-francisco"
    },
    {
      "id": 14726,
      "tag_type": "RoleTag",
      "name": "developer",
      "display_name": "Developer",
      "angellist_url": "http://angel.co/developer"
    }
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2015-01-19 03:56:00

在我看来,这是一个多态关联:

basics.html#polymorphic-associations

代码语言:javascript
复制
class Tag < ActiveRecord::Base
  belongs_to :taggable, polymorphic: true
end

class Company < ActiveRecord::Base
  has_many :tags, as: :taggable
end

class Job < ActiveRecord::Base
  has_many :tags, as: :taggable
end

class Person < ActiveRecord::Base
  has_many :tags, as: :taggable
end

class CreateTags < ActiveRecord::Migration
  def change
    create_table :tags do |t|
      t.integer :taggable_id
      t.string :taggable_type
      t.string :tag_type
      t.string :name
      t.string :display_name
      t.string :angellist_url  
    end
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28015745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档