简体   繁体   English

创建联接模型时,有没有一种清晰的Rails方式来设置属性?

[英]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.

相关问题 Rails API:当嵌套属性设置了ID时,创建转为更新。 有什么办法可以避免这种情况? - Rails API: Creating turns into updating when nested attributes have an ID set. Any way to avoid this? Rails 3.1-编辑联接模型中的属性? - Rails 3.1 - Editing Attributes in join models? 在rails中创建联接表; 当添加索引不应该是两种方式 - creating a join table in rails; when adding indices should not they be two way 在给定大数据集的情况下,在Rails中搜索通过连接表连接的两个模型的最快方法 - Fastest way to search two models connected via join table in rails given large data set 在Rails中创建具有一个模型和另一个模型上的两个属性的连接表 - Creating Join Table in Rails with One Model and Two Attributes on Another Model Rails 3三个模型加入 - Rails 3 Three Models Join 当更改后的属性在“回调后”内是干净的时,如何依赖脏模型? - How to rely on Dirty models when the changed attributes are clean within “after callbacks”? 在Rails中访问Join属性4 - Accessing Join Attributes in Rails 4 Rails:当属性设置为true时显示帖子 - Rails: show post when attributes is set to true Rails:将模型结构化为子文件夹而不创建子模块的优雅方式 - Rails: Elegant way to structure models into subfolders without creating submodules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM