# Simple active record issue. I must be missing something

**URL:** <https://www.ruby-forum.com/t/simple-active-record-issue-i-must-be-missing-something/134100>\
**Category:** Rails\
**Created:** [April 7, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/simple-active-record-issue-i-must-be-missing-something/134100 "2008-04-07T21:44:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [April 7, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/simple-active-record-issue-i-must-be-missing-something/134100/1 "2008-04-07T21:44:50Z")

</div>

Here’s a seemingly simple ActiveRecord use case. I have Projects and  
Users. Each project has a single team leader as well as a review team  
that consists of several users. I would like to use the same User  
model for authentication and just reference these users in my Project  
model.

I tried this but got an “unknown column” error (for User.project\_id)

class Project  
has\_one :lead, :class\_name =\> ‘User’  
end

Any suggestions on how to model this relationship without requiring  
references to Project in the User table?

From a database table viewpoint I would like

```
create_table :projects do |t|
  t.integer :lead_id
  t.timestamps
end

```

The review team part seems like it could be done with HBTM.

Any thoughts?

Sean

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [April 7, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/simple-active-record-issue-i-must-be-missing-something/134100/2 "2008-04-07T21:49:11Z")

</div>

schof wrote:

> Here’s a seemingly simple ActiveRecord use case. I have Projects and  
> Users. Each project has a single team leader as well as a review team  
> that consists of several users. I would like to use the same User  
> model for authentication and just reference these users in my Project  
> model.
> 
> I tried this but got an “unknown column” error (for User.project\_id)
> 
> class Project  
> has\_one :lead, :class\_name =\> ‘User’  
> end
> 
> Any suggestions on how to model this relationship without requiring  
> references to Project in the User table?
> 
> From a database table viewpoint I would like
> 
> ```
> create_table :projects do |t|
> t.integer :lead_id
> t.timestamps
> end
> 
> ```
> 
> The review team part seems like it could be done with HBTM.
> 
> Any thoughts?
> 
> Sean

You want belongs\_to.

belongs\_to :lead, :class\_name =\> ‘User’

And, because I forget of the foreign maps to the association name, or  
the class name, you may need that to be:

belongs\_to :lead, :class\_name =\> ‘User’, :foreign\_key =\> ‘lead\_id’

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [April 7, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/simple-active-record-issue-i-must-be-missing-something/134100/3 "2008-04-07T22:36:02Z")

</div>

Of course belongs\_to! Thanks. (I told you it was something  
obvious.) I think I was getting hung up on :class\_name which I have  
not used before.

Sean

On Apr 7, 3:49 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
