Reputation: 946
I have the following models:
class Site
has_many :posts
end
class Post
belongs_to :site
end
And the post has the following columns:
id
site_id
url
title
body
This is going to store wordpress posts, and I want to use the post_id
of wordpress post, and because of that I could have two sites that would have the post 1
for example. So I need a way to have duplicated post ids, but pointing to different entries.
Thank you.
Upvotes: 1
Views: 32
Reputation: 9523
Regardless of this use case, it's a bad practice to make your ActiveRecord id
rely on another data source.
For your case, ignore the conventional id
and store the WordPress post ID in a new column, let's call it wp_id
, which is not unique across the table.
Then, you can create the following index to make queries faster and assure the uniqueness of post ID per site:
add_index :posts, [:site_id, :wp_id], unique: true
This way, you get what's called a composite key, means every combination of both site_id
and wp_id
is unique across the posts
table.
Upvotes: 1