【问题标题】:Rails Model Setup RolesRails 模型设置角色
【发布时间】:2013-08-25 04:49:19
【问题描述】:

我正在开发一个 Rails 4 应用程序,我有两个模型开发人员和应用程序。一个开发者可以有很多应用,并且属于很多应用。 (一个应用程序的多个开发人员)我已经成功设置了一个连接表并且一切正常,但我想扩展它,以便开发人员要么是一个可以编辑应用程序的:创始人,要么是一个只能访问视图的合作者应用程序。有什么最好的建议是实现这个功能吗?

应用程序

  has_and_belongs_to_many :collaborators, class_name: 'Developer', association_foreign_key: :collaborator_id
  has_and_belongs_to_many :founders, class_name: 'Developer', association_foreign_key: :founder_id

开发者

  has_and_belongs_to_many :apps, foreign_key: 'collaborator_id'
  has_and_belongs_to_many :apps, foreign_key: 'founder_id'

【问题讨论】:

  • 你是如何定义AppDeveloperhas_many throughhas_and_belongs_to_many的关系的?
  • @j03w 我更新了我的问题
  • 您有 2 个选择 1. 创建一个类似于 @zolter 回答建议的新表并使用 has_many through 而不是 hbtm 在控制器中检查 editupdate 方法. 2.修改你的hbtm成为一个合适的模型表:添加迁移以创建时间戳列stackoverflow.com/questions/7542976/…并将role列添加到表中。然后在你的models中创建model文件

标签: ruby-on-rails activerecord ruby-on-rails-4 activemodel


【解决方案1】:

我认为您需要创建另一个表:

  def change
    create_table :access_restricts do |t|
      t.integer :user_id
      t.integer :application_id
      t.integer :role_id

      t.timestamps
    end
  end

用户和应用程序将有许多评估限制。

【讨论】:

    【解决方案2】:

    我建议在这种情况下使用单表继承。

    STI

    STI SAMPLE

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多