[英]Is there a clean Rails way to set attributes when creating join models?
I'm using Rails 3 and have a rich association between Projects and Users. 我正在使用Rails 3,并且在项目和用户之间有丰富的关联。 The join model ( UsersProject
) has an additional boolean attribute, administrator
, which flags the User
as an admin of Project
: UsersProject
模型( UsersProject
)有一个附加的布尔属性administrator
,将User
标记为Project
的管理员:
Sure I'm missing something obvious here, but is there a clean way to set the administrator
attribute on the join model ( UsersProject
) when creating a new Project? 当然,我在这里遗漏了一些明显的东西,但是在创建新项目时是否有一种干净的方法来在连接模型( UsersProject
)上设置administrator
属性? eg: 例如:
class Project < ActiveRecord::Base
has_many :users_projects
has_many :users, :through => :users_projects
end
class User < ActiveRecord::Base
has_many :users_projects
has_many :projects, :through => :users_projects
# Guessing I use something along these lines, although should I be using scope?
# has_many :administered_projects,
# :through => :users_projects,
# :source => :project,
# :conditions => ['users_projects.administrator = ?', true]
# ...
end
class UsersProject < ActiveRecord::Base
# Join model has an boolean attribute :administrator
belongs_to :user
belongs_to :project
end
# INTENDED USAGE:
@project = @user.administered_projects.new(params[:project])
# => Creates a UsersProject record with `:administrator=true`
@project = @user.projects.new(params[:project])
# => Creates a UsersProject record with `:administrator=false`
Appreciate any help, Chris 克里斯,谢谢您的帮助
While not directly answering your question, the implementation seems a bit clunky to me. 虽然没有直接回答您的问题,但对我来说,实现似乎有些笨拙。
If you had users and projects, the answer would be simple--you could just create a has_and_belongs_to_many
relationship between the two models, and create a join table. 如果您有用户和项目,答案将很简单-您可以在两个模型之间创建has_and_belongs_to_many
关系,然后创建一个has_and_belongs_to_many
表。 The administrator flag makes the situation slightly more tricky, but I'm not sure having a join model would be necessary. 管理员标志使情况更加棘手,但是我不确定是否需要加入模型 。
Personally, I'd simply set up two relationships between the models, like this: 就个人而言,我只需在模型之间建立两个关系,如下所示:
class Project < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :administrators, :class_name => 'User', :join_table => 'administrators_projects' # potentially some foreign_key directives here as well
end
class User < ActiveRecord::Base
has_and_belongs_to_many :projects
has_and_belongs_to_many :administered_projects, :class_name => 'Project', :join_table => 'administrators_projects' # potentially some foreign_key directives here as well
end
This way, you'll still only have your two models, but with all the functionality you'd need (and your "INTENDED USAGE" structures would work). 这样,您仍将只有两个模型,但具有所需的所有功能(并且“预期用途”结构将起作用)。 If you wanted to, you could add the administrators to the projects_users relationship as well (so that admins are both admins and users), but that would be completely up to how you want to structure the rest of the table. 如果愿意,也可以将管理员添加到projects_users关系中(这样,管理员既是管理员又是用户),但这完全取决于您要如何构造表的其余部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.