# Error when trying to access model's parent

**URL:** <https://www.ruby-forum.com/t/error-when-trying-to-access-models-parent/85619>\
**Category:** Rails\
**Created:** [February 14, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/error-when-trying-to-access-models-parent/85619 "2007-02-14T23:19:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [February 14, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/error-when-trying-to-access-models-parent/85619/1 "2007-02-14T23:19:21Z")

</div>

I have two database backed models with a one to many relationship. When  
I try to access one of the parent’s attributes through the child I get  
the following error:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column  
‘staff\_members.student\_id’ in ‘where clause’: SELECT \* FROM  
staff\_members WHERE (staff\_members.student\_id = 352) LIMIT 1

Here are my models:

class StaffMember \< ActiveRecord::Base  
has\_many :students  
…  
end

class Student \< ActiveRecord::Base  
has\_one :staff\_member  
…  
end

Given this relationship I wouldn’t think that the staff\_members table  
would (or should) have a student\_id field. The students table does  
however have a staff\_member\_id table which is properly populated.

In my controller I’m setting an instance variable.  
@student = Student.find(params[:id])

In the view I’m trying to access the parent’s attribute  
\<%= @student.staff\_member.first\_name %\>

What am I doing wrong?

Thanks,  
Jake

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [February 14, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/error-when-trying-to-access-models-parent/85619/2 "2007-02-14T23:28:52Z")

</div>

Are you sure “student\_id” is the foreign key? I had a similar problem  
the other day.

On Feb 14, 5:19 pm, Jake S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [February 14, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/error-when-trying-to-access-models-parent/85619/3 "2007-02-14T23:55:54Z")

</div>

A one-to-many relationship is set up as so:

class StaffMember \< ActiveRecord::Base  
has\_many :students  
…  
end

class Student \< ActiveRecord::Base  
belongs\_to :staff\_member  
…  
end

belongs\_to( ) declares that the given class has a parent relationship  
to the class  
containing the declaration - so staffmember is the parent of student

Your student table needs to have a foreign\_key called: staff\_member\_id

I would change your controller as such, so that the logic is in the  
controller and not the view:  
@student = Student.find(params[:id])  
@staff\_name = @student.staffmember.first\_name

Then in your view  
\<%= @staff\_name%\>

Hope this helps -K

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [February 15, 2007, 12:03am UTC](https://www.ruby-forum.com/t/error-when-trying-to-access-models-parent/85619/4 "2007-02-15T00:03:38Z")

</div>

Kim wrote:

> A one-to-many relationship is set up as so:
> 
> class StaffMember \< ActiveRecord::Base  
> has\_many :students  
> …  
> end
> 
> class Student \< ActiveRecord::Base  
> belongs\_to :staff\_member  
> …  
> end

Sorry, the “has\_one” was a typo. I didn’t setup a FK, I will try that.

Thanks,  
Jake
