views:

25

answers:

1

I have two models, users and themes, that I'm currently joining in a HABTM association in a themes_users table. Basically, after a user creates a new theme, it becomes available to other users to select for their own use.

However, only the original creator of the theme should have the ability to edit it. So, I need to have some other kind of association to handle that relationship, something like an created_by_id field in the theme.

In this way, the user model acts as two different roles: they can be an implementer of a theme AND/OR the owner of the theme. The implementer relationship is handled by the themes_users join table; the question is: What is the right way to handle this secondary association? Do I need to make users polymorphic and then make the created_by_id reference an "owner"? Or is there something easier?

Thanks so much for your help!

A: 

I believe you should add the following association

class User < ApplicationController
   # a user can create many themes
   has_many :themes_created, :foreign_key => :creator_id, :class_name => "Theme"
end

class Theme < ApplicationController
   # add a creator_id column in your themes table
   belongs_to :creator, :class_name => "User"
end

This way you can get all the themes created by some @user through

@user.themes_created
j.
The problem is that because of the HABTM association that already exists via themes_users, there's already a "has_many :themes" and "user.themes" on the user model.
Just use another name then. See my edit.
j.
But you still can't use "user.themes"... Is it as simple as doing the same with the user class? Like, in User "has_many :mythemes, :class_name => 'Theme'" and calling via "user.mythemes" ? I doesn't seem like that should work...
Yes, that's what you have to do. If you look at my answer, I've used `@user.themes_created`. This works.
j.
This totally works. Thanks so much.
You're welcome! :]
j.