什么关联应该用于"标签"在Rails?

时间:2015-01-18 22:52:15

标签: ruby-on-rails rails-activerecord

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

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

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

公司的JSON示例:

{
  "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示例:

{
  "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"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

看起来像我的多态关联:

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

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