Simple active record issue. I must be missing something

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

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’

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. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs