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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Are you sure "student_id" is the foreign key? I had a similar problem the other day. On Feb 14, 5:19 pm, Jake Schutz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Feb 14, 2:27 pm, "blakeage" <blakeleemil...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Are you sure "student_id" is the foreign key? I had a similar problem > the other day. > > On Feb 14, 5:19 pm, Jake Schutz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > 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 > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 > ... > endSorry, the "has_one" was a typo. I didn''t setup a FK, I will try that. Thanks, Jake -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---