Ruby on Rails - проблема ActiveRecord, миграции и двух столбцов в одну таблицу - PullRequest
0 голосов
/ 05 марта 2011

Хорошо, у меня есть таблица, которая отслеживает историю «персоны», она регистрирует человека (пользователя), обработчика (пользователя), состояние до (JobApplicationStatus), состояние после (JobApplicationStatus).

Теперь в моей голове это выглядит так:

**JobApplicationHistory**
id (int)
status_before_id (int)
status_after_id (int)
user_id (int)
handler_id (int)

Я пытался сделать миграцию, чтобы сорта работали, но она не работает правильно.

Как я хотел бы использовать что-то вроде:

user = User.find(1)
handler = User.find(1)
status_before = JobApplicationStatus.find(1)
status_after = JobApplicationStatus.find(2)

history = JobApplicationHistory.new()
history.user = user
history.handler = handler
history.status_before = status_before
history.status_after = status_after
history.save

Вот моя миграция

<pre> class CreateUserApplicationHistories < ActiveRecord::Migration def self.up create_table :user_application_histories do |t| t.integer :user_id # goes to User t.references :job # goes to Job t.integer :handler_id # goes to User t.integer :status_from_id # goes to JobApplicationStatus t.integer :status_to_id # goes to JobApplicationStatus t.timestamps end</p> <pre><code>add_index("user_application_histories", "job_id") add_index("user_application_histories", "handler_id") add_index("user_application_histories", "user_id") add_index("user_application_histories", "status_from_id") add_index("user_application_histories", "status_to_id")

конец

def self.down drop_table: user_application_histories конец конец

И моя модель, которая, как мне кажется, дает сбой

<pre> class UserApplicationHistory < ActiveRecord::Base belongs_to :status_from_id, :class_name => "JobApplicationStatus" belongs_to :status_to_id, :class_name => "JobApplicationStatus" belongs_to :user_id, :class_name => "User" belongs_to :handler_id, :class_name => "User" end

1 Ответ

1 голос
/ 05 марта 2011

Вы правы, ваша модель должна выглядеть так для пользователя и обработчика:

belongs_to :user
belongs_to :handler, :class_name => "User"

Чтобы помочь вам с JobApplicationStatus, мне нужно знать, как выглядит эта таблица

...