开发者

Two Different Types of Associatons on the Same Two Tables in Rails

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 associa开发者_Go百科tion? 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!


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜