I have users and projects. User can have many projects and project can have many users. Now project has a particular user as an author or the creator, ie. project has one creator and that creator is also a user. How do I map this relation in rails !!!
2 に答える
0
#Project
has_and_belongs_to_many :users
belongs_to :author, :class_name => "User"
#User
has_and_belongs_to_many :projects
has_many :authored_projects, :class_name => "Project", :foreign_key => "author_id"
author_id
そして、プロジェクト テーブルに追加する必要があります。
したがって、次を使用できます: project.author
、user.authored_projects
、など
于 2012-05-24T09:29:55.177 に答える
0
これを試して
class User < ActiveRecord::Base
has_and_belongs_to_many :projects
has_many :created_projects, :class_name => "Project", :foreign_key => "creator_id"
end
class Project< ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :creator, :class_name => "User"
end
于 2012-05-24T09:49:37.033 に答える