Bill Christian
Bill Christian

Reputation: 689

I need to specify a field name different than the table for association

Given I have an Artifact model and a User model: I would like to define two Artifact fields, opened_by and assigned_to, who values are User ids and inherit all of the proper association methods.

What is the proper belongs_to or has_one or has_many options I should set?

The goal is to be able to reference the user's name through the statement hld.assiged_to.name where hld is an artifact.

Thanks for the help. I've gotten myself confused with terminology with all of the reading i've done on the problem.

Upvotes: 10

Views: 4329

Answers (1)

Bill Christian
Bill Christian

Reputation: 689

The following is what I determined was correct.

class Artifact < ActiveRecord::Base
belongs_to :project
belongs_to :opened_by, :class_name => 'User'
belongs_to :assigned_to, :class_name => 'User'

The first argument in the belongs_to specifies the field to reference. The second indicates the model/class to use as the reference.

Upvotes: 18

Related Questions