Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 385
Bill Christian Avatar asked Oct 29 '10 20:10

Bill Christian


1 Answers

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.

like image 175
Bill Christian Avatar answered Nov 09 '22 10:11

Bill Christian