0

Okay so what I have is a table that keeps track of history on a "person", this logs the person(User), the handler(User), the status before(JobApplicationStatus), the status after(JobApplicationStatus).

Now in my head this translates down to a table of such:

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

I tried to make a migation, that sorta worked, but it's not working right.

As I would like to use something like:

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

Here is my migration

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

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")

end

def self.down drop_table :user_application_histories end end

And my model, which i think makes it fail

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

2
  • What is the error/failure that you are seeing? Commented Mar 4, 2011 at 21:13
  • It's not assigning anything, the status_before and the others remains null Commented Mar 4, 2011 at 21:15

1 Answer 1

1

You right, your model should look like this for user and handler:

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

To help you with JobApplicationStatus I would need to know how this table looks like

Sign up to request clarification or add additional context in comments.

2 Comments

The foreign_key is implicit. You should only use that option if it's not [association_name]_id.
@Ryan, your right, i've edited my answer to make it better ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.