On 7/3/05, Justin Palmer <rails-JJvkDrV5TXAmlAP/+Wk3EA@public.gmane.org>
wrote:> I''d like to get some opinions on how others tackle this type of
> situation in Rails.
> 
> To set the scene, I have a ''User'' and
''Post'' and these are both habtm
> relations to each other.
> 
> In the association table:
> user_id: 1
> post_id : 6
> 
> What I would like to do now is allow entries(comments/blogs/etc) on
> this relation.  So if Sue wanted to comment on John''s (user_id 1)
> post (post_id 6), what would be the best way to handle this in Rails?
> It seems that in my entry table I would need something like:
> 
> id
> user_id
> post_id
> title
> body
> ....
> 
> 
> FYI, a post can belong to any number of users (much like a
''thing'' on
> 43 things), this is why the relations are set up as so.
> 
> Any comments/suggestions on how you would handle this?
> 
> Cheers,
>   -Justin Palmer
Hi Justin,
What would be best is if you give each association an ID as in
CREATE TABLE user_posts (
  id SERIAL PRIMARY KEY,
  user_id INTEGER REFERENCES users(id), 
  post_id INTEGER REFERENCES posts(id)
);
Then, keying from your entries table becomes a lot easier and better
supported by Rails. (Rails doesn''t support multivalued foreign/primary
keys, so you don''t really have that much of a choise.)
CREATE TABLE entries (
  id SERIAL PRIMARY KEY,
  user_post_id INTEGER REFERENCES user_posts(id),
  title TEXT,
  body TEXT
);
Hoping this helps,
 - Rowan
-- 
Morality is usually taught by the immoral.