I am trying to build a small database of Universities, Departments and Courses. But for some reason I can''t seem to get a list of courses attached to a Department. So far I have the following Database: => Course(id: integer, course_name: string, length: integer, description: text, programme: string, department_id: integer, university_id: integer, created_at: datetime, updated_at: datetime) => Department(id: integer, department_name: string, department_description: text, head_firstname: string, head_lastname: string, head_email: string, university_id: integer, courses_id: integer, created_at: datetime, updated_at: datetime) => University(id: integer, name: string, website: string, country: string, city: string, departments_id: integer, courses_id: integer, created_at: datetime, updated_at: datetime) Models: class Course < ActiveRecord::Base belongs_to :department belongs_to :university end class Department < ActiveRecord::Base belongs_to :university has_many :courses end class University < ActiveRecord::Base has_many :departments has_many :courses end For the Department I am trying to run the following controller def index @departments = Department.includes(:courses, :university) and the following view: <% @departments.each do |department| %> <tr> <td><%= department.department_name %></td> <td><%= department.department_description %></td> <td><%= department.head_firstname %></td> <td><%= department.head_lastname %></td> <td><%= department.head_email %></td> <td><%= department.university.name %></td> <td><%= department.courses %> that last line outputs #<Course:0x1042b8270> and it fails when i try to add something like department.courses.course_name I am at a loss here and haven''t been able to find any clues on the documentation as to what I might be doing wrong. If anyone could at least point me in the right direction, that would be great. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 28, 9:07 pm, Bruno Amaral <m...-S2ydJG5fPKHivB4pSPl/wg@public.gmane.org> wrote:> <td><%= department.courses %> > > that last line outputs #<Course:0x1042b8270> and it fails when i try > to add something like department.courses.course_name > > I am at a loss here and haven''t been able to find any clues on the > documentation as to what I might be doing wrong. If anyone could at > least point me in the right direction, that would be great.Because department.courses is a collection of courses and doesn''t have a course_name attribute (although the objects in that collection will do). You either need to pick which course you want to manipulate all iterate over them (similarly, you can''t do @departments.department_name - you had to iterate over the departments in that collection) Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I suspect department.courses returns and array and when you print it out you get the address of the first entry in the array. That would also explain department.courses.course_name would fail because you have to index into the department.courses array like department.courses[x].course_name to be able to set it On Nov 28, 1:07 pm, Bruno Amaral <m...-S2ydJG5fPKHivB4pSPl/wg@public.gmane.org> wrote:> I am trying to build a small database of Universities, Departments and > Courses. But for some reason I can''t seem to get a list of courses > attached to a Department. > > So far I have the following Database: > => Course(id: integer, course_name: string, length: integer, > description: text, programme: string, department_id: integer, > university_id: integer, created_at: datetime, updated_at: datetime) > > => Department(id: integer, department_name: string, > department_description: text, head_firstname: string, head_lastname: > string, head_email: string, university_id: integer, courses_id: > integer, created_at: datetime, updated_at: datetime) > > => University(id: integer, name: string, website: string, country: > string, city: string, departments_id: integer, courses_id: integer, > created_at: datetime, updated_at: datetime) > > Models: > class Course < ActiveRecord::Base > belongs_to :department > belongs_to :university > end > class Department < ActiveRecord::Base > belongs_to :university > has_many :courses > end > class University < ActiveRecord::Base > has_many :departments > has_many :courses > end > > For the Department I am trying to run the following controller > def index > @departments = Department.includes(:courses, :university) > > and the following view: > <% @departments.each do |department| %> > <tr> > <td><%= department.department_name %></td> > <td><%= department.department_description %></td> > <td><%= department.head_firstname %></td> > <td><%= department.head_lastname %></td> > <td><%= department.head_email %></td> > <td><%= department.university.name %></td> > <td><%= department.courses %> > > that last line outputs #<Course:0x1042b8270> and it fails when i try > to add something like department.courses.course_name > > I am at a loss here and haven''t been able to find any clues on the > documentation as to what I might be doing wrong. If anyone could at > least point me in the right direction, that would be great.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 28 November 2010 21:07, Bruno Amaral <mail-S2ydJG5fPKHivB4pSPl/wg@public.gmane.org> wrote:> I am trying to build a small database of Universities, Departments and > Courses. But for some reason I can''t seem to get a list of courses > attached to a Department. > > So far I have the following Database: > => Course(id: integer, course_name: string, length: integer, > description: text, programme: string, department_id: integer, > university_id: integer, created_at: datetime, updated_at: datetime) > > => Department(id: integer, department_name: string, > department_description: text, head_firstname: string, head_lastname: > string, head_email: string, university_id: integer, courses_id: > integer, created_at: datetime, updated_at: datetime) > > => University(id: integer, name: string, website: string, country: > string, city: string, departments_id: integer, courses_id: integer, > created_at: datetime, updated_at: datetime) > > Models: > class Course < ActiveRecord::Base > belongs_to :department > belongs_to :universityYou don''t need course belongs_to university, because course belongs_to department and department belongs_to university. So the university for a course is course.department.university. That is on the assumption that the university the course belongs to is the same one that it''s department belongs to.> end > class Department < ActiveRecord::Base > belongs_to :university > has_many :courses > end > class University < ActiveRecord::Base > has_many :departments > has_many :courses > end > > For the Department I am trying to run the following controller > def index > @departments = Department.includes(:courses, :university) > > and the following view: > <% @departments.each do |department| %> > <tr> > <td><%= department.department_name %></td> > <td><%= department.department_description %></td> > <td><%= department.head_firstname %></td> > <td><%= department.head_lastname %></td> > <td><%= department.head_email %></td> > <td><%= department.university.name %></td> > <td><%= department.courses %> > > that last line outputs #<Course:0x1042b8270> and it fails when i try > to add something like department.courses.course_nameI see Fred has pointed out the problem here already. Colin> > I am at a loss here and haven''t been able to find any clues on the > documentation as to what I might be doing wrong. If anyone could at > least point me in the right direction, that would be great. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Skt, Fred, Collin, thank you! The following code was enough to get the view working: <% department.courses.each do |course| %> <%= course.course_name %> <% end %> I am very new to both ruby and rails so I tend to miss a few key concepts, as was the case. Thank you again :) On Nov 28, 9:48 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 28 November 2010 21:07, Bruno Amaral <m...-S2ydJG5fPKHivB4pSPl/wg@public.gmane.org> wrote: > > > > > > > I am trying to build a small database of Universities, Departments and > > Courses. But for some reason I can''t seem to get a list of courses > > attached to a Department. > > > So far I have the following Database: > > => Course(id: integer, course_name: string, length: integer, > > description: text, programme: string, department_id: integer, > > university_id: integer, created_at: datetime, updated_at: datetime) > > > => Department(id: integer, department_name: string, > > department_description: text, head_firstname: string, head_lastname: > > string, head_email: string, university_id: integer, courses_id: > > integer, created_at: datetime, updated_at: datetime) > > > => University(id: integer, name: string, website: string, country: > > string, city: string, departments_id: integer, courses_id: integer, > > created_at: datetime, updated_at: datetime) > > > Models: > > class Course < ActiveRecord::Base > > belongs_to :department > > belongs_to :university > > You don''t need course belongs_to university, because course belongs_to > department and department belongs_to university. So the university > for a course is course.department.university. That is on the > assumption that the university the course belongs to is the same one > that it''s department belongs to. > > > > > > > end > > class Department < ActiveRecord::Base > > belongs_to :university > > has_many :courses > > end > > class University < ActiveRecord::Base > > has_many :departments > > has_many :courses > > end > > > For the Department I am trying to run the following controller > > def index > > @departments = Department.includes(:courses, :university) > > > and the following view: > > <% @departments.each do |department| %> > > <tr> > > <td><%= department.department_name %></td> > > <td><%= department.department_description %></td> > > <td><%= department.head_firstname %></td> > > <td><%= department.head_lastname %></td> > > <td><%= department.head_email %></td> > > <td><%= department.university.name %></td> > > <td><%= department.courses %> > > > that last line outputs #<Course:0x1042b8270> and it fails when i try > > to add something like department.courses.course_name > > I see Fred has pointed out the problem here already. > > Colin > > > > > > > I am at a loss here and haven''t been able to find any clues on the > > documentation as to what I might be doing wrong. If anyone could at > > least point me in the right direction, that would be great. > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
It''d be interesting if AssociationProxy added in pluralised attribute names as easy go-to''s for mappings, hey? for example: department.courses.course_names (yields the same result as:...) department.courses.map{|course| course.course_name} ---------------------------------------------- Buy my new album! 16 Sep 2010: http://itunes.apple.com/au/album/erste-zeit/id393326346 On 29/11/2010, at 8:46 AM, skt wrote:> I suspect department.courses returns and array and when you print it > out you get the address of the first entry in the array. > > That would also explain department.courses.course_name would fail > because you have to index into the department.courses array like > department.courses[x].course_name to be able to set it > > > On Nov 28, 1:07 pm, Bruno Amaral <m...-S2ydJG5fPKHivB4pSPl/wg@public.gmane.org> wrote: >> I am trying to build a small database of Universities, Departments and >> Courses. But for some reason I can''t seem to get a list of courses >> attached to a Department. >> >> So far I have the following Database: >> => Course(id: integer, course_name: string, length: integer, >> description: text, programme: string, department_id: integer, >> university_id: integer, created_at: datetime, updated_at: datetime) >> >> => Department(id: integer, department_name: string, >> department_description: text, head_firstname: string, head_lastname: >> string, head_email: string, university_id: integer, courses_id: >> integer, created_at: datetime, updated_at: datetime) >> >> => University(id: integer, name: string, website: string, country: >> string, city: string, departments_id: integer, courses_id: integer, >> created_at: datetime, updated_at: datetime) >> >> Models: >> class Course < ActiveRecord::Base >> belongs_to :department >> belongs_to :university >> end >> class Department < ActiveRecord::Base >> belongs_to :university >> has_many :courses >> end >> class University < ActiveRecord::Base >> has_many :departments >> has_many :courses >> end >> >> For the Department I am trying to run the following controller >> def index >> @departments = Department.includes(:courses, :university) >> >> and the following view: >> <% @departments.each do |department| %> >> <tr> >> <td><%= department.department_name %></td> >> <td><%= department.department_description %></td> >> <td><%= department.head_firstname %></td> >> <td><%= department.head_lastname %></td> >> <td><%= department.head_email %></td> >> <td><%= department.university.name %></td> >> <td><%= department.courses %> >> >> that last line outputs #<Course:0x1042b8270> and it fails when i try >> to add something like department.courses.course_name >> >> I am at a loss here and haven''t been able to find any clues on the >> documentation as to what I might be doing wrong. If anyone could at >> least point me in the right direction, that would be great. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Julian Leviston wrote in post #964556:> It''d be interesting if AssociationProxy added in pluralised attribute > names as easy go-to''s for mappings, hey?No. What would be the point?> > for example: > > department.courses.course_names > > (yields the same result as:...) > > department.courses.map{|course| course.course_name}Why? So you don''t have to think about what you''re doing? Or is there a good reason that escapes me?> > > ---------------------------------------------- > Buy my new album! 16 Sep 2010: > http://itunes.apple.com/au/album/erste-zeit/id393326346Cool! Nice to see other musicians here. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.