1

I'm working on a rails 4 app and i have two models Developers and Apps. A developer can have many apps and belongs to many apps. (Multiple developers for an app) I have successfully setup a joining table and everything works but i want to extent this so that a developer is either a :founder where he can have access to edit the app or a collaborator who only has access to view the app. Any suggestions on the best possible was to achieve this functionality?

App

  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

Developer

  has_and_belongs_to_many :apps, foreign_key: 'collaborator_id'
  has_and_belongs_to_many :apps, foreign_key: 'founder_id'
ny95
  • 680
  • 5
  • 17
  • how did you define relationship on `App` and `Developer` through `has_many through` or `has_and_belongs_to_many`? – j03w Aug 25 '13 at 06:56
  • You have 2 choices 1. Create a new table similar to what @zolter answer suggested and use `has_many through` instead of `hbtm` have some check in `edit` and `update` methods in your controller. 2. modify your `hbtm` to become a proper model table: add migration to create timestamps columns http://stackoverflow.com/questions/7542976/add-timestamps-to-an-existing-table and add `role` column to the table as well. Then create the `model` file in your `models` – j03w Aug 25 '13 at 07:22

2 Answers2

1

I think you need create another table:

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

      t.timestamps
    end
  end

User and application will have many assess_restricts.

zolter
  • 7,070
  • 3
  • 37
  • 51
0

I would suggest to use single table inheritance in this case.

STI

STI SAMPLE

Muntasim
  • 6,689
  • 3
  • 46
  • 69