简体   繁体   中英

What association should be used for “tags” in Rails?

AngelList.co has an API that returns data about its companies, jobs, and people.

Each company, job, and person record can have several "tags" with information like "skill", "location", "role", "market", etc.

How would these "tags" be modeled in Rails?

Example JSON for a Company:

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

Example JSON for a Job:

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

Looks like a polymorphic association to me:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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