Вопрос

Rails 2.1.0 (на данный момент не может обновляться из -за нескольких ограничений) Я пытаюсь достичь этого. Какие -нибудь подсказки?

  1. Проект имеет много пользователей через модель присоединения
  2. У пользователя есть много проектов через модель JOIN
  3. Класс администратора наследует пользовательский класс. У этого также есть некоторые конкретные администраторы.
  4. Администратор нравится наследование для руководителя и оператора
  5. Проект имеет одного администратора, одного руководителя и многих операторов.

Теперь я хочу 1. Отправить данные для проекта, администратора, руководителя и оператора в одной форме проекта 2. Проверьте все и покажите ошибки в форме проекта.

Project has_many :projects_users ; has_many :users, :through => :projects_users
User has_many :projects_users ; has_many :projects, :through => :projects_users
ProjectsUser = :id integer, :user_id :integer, :project_id :integer
ProjectUser belongs_to :project, belongs_to :user
Admin < User # User has 'type:string' column for STI
Supervisor < User
Operator < User

Подход правильный? Любые и все предложения приветствуются.

Это было полезно?

Решение 3

В итоге я использовал виртуальные атрибуты для администратора, руководителя и операторов, все остальное прошло через ассоциации ORM

Другие советы

Я предполагаю, что единственная таблица и вложенные формы могут помочь вам здесь. Возможно, вы захотите прочитать об этом в документации. http://api.rubyonrails.org/classes/activerecord/base.html

Я понял, что ответ заключается в наследстве на одной таблице с has_many: через

class Project < ActiveRecord::Base
  has_many :projects_users, :dependent => :destroy
  has_many :users, :through => :projects_users, :dependent => :destroy
  has_one :admin, :through => :projects_users, :source => :user, :conditions => {:type => "Admin"}
  has_one :supervisor, :through => :projects_users, :source => :user, :conditions => {:type => "Supervisor"}
  has_many :operators, :through => :projects_users, :source => :user, :conditions => {:type => "Operator"}
  validates_presence_of :name
  validates_associated :admin

  def admin_attributes=(attributes)
    # build an admin object and apply to this relation
  end

  def supervisor_attributes=(attributes)
    # build a supervisor object and apply to this relation
  end

  def operator_attributes=(operator_attributes)
    operator_attributes.each do |attributes|
      # build an operator object for each set of attributes and apply to this relation
    end
  end
end

class User < ActiveRecord::Base
  has_many :projects_users, :dependent => :destroy
  has_many :projects, :through => :projects_users, :dependent => :destroy
  validates_presence_of :name
  validates_associated :projects
end

class ProjectsUser < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
end

class Admin < User
end

class Supervisor < User
end

class Operator < User
end

Теперь мы можем иметь следующее

project1.users # User objects for admin, supervisor and operators
project1.admin # User object for Admin
project1.supervisor # User object for Supervisor
project1.operators # User objects for Operator

Сложная форма, включая все это может иметь

<% form_for :project ...
    <% fields_for "project[admin_attributes]", @project.admin do |admin_form|
    ...
    <% @project.operators.each do |operator| %>
        <% fields_for "project[operator_attributes][]", operator do |operator_form| %>

и так далее...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top