Hi, I''m a little stuck here, will appreciate some help. I have a list method in the controller, which gets a bunch of records from a table and displays them. One of them is a status_id field. I got this to display properly by editing the generated list.rhtml. But, what I want is the status name itself to be displayed, which comes from the ''status'' table. Is there a simple way to do this? Thanks, Vamsee.
Vamsee Kanakala wrote:> Hi, > > I''m a little stuck here, will appreciate some help. I have a list > method in the controller, which gets a bunch of records from a table and > displays them. One of them is a status_id field. I got this to display > properly by editing the generated list.rhtml. But, what I want is the > status name itself to be displayed, which comes from the ''status'' table. > Is there a simple way to do this?def list @things = Thing.find(params[:id], :include=>:status) end and <% @things.each do |thing| %> Name: <%= thing.name %> Status: <%= thing.status.name %> <% end %> should do the trick... -- Alex
Alex Young wrote:>def list > @things = Thing.find(params[:id], :include=>:status) >end > >and > ><% @things.each do |thing| %> >Name: <%= thing.name %> >Status: <%= thing.status.name %> ><% end %> > >Thanks Alex, But in my case it''s not so straight forward - I want to do pagination too, so I did something like this (borrowed from http://wiki.rubyonrails.com/rails/pages/PaginationHelper): def list @requirement_pages = Paginator.new self, SearchRequirement.count, 10, @params[''page''] @requirements = SearchRequirement.find(:all, :include => :status) end And says something like this: ERROR: column status.search_requirement_id does not exist : SELECT search_requirements.location AS t0_r6, search_requirements.status_id AS t0_r7, search_requirements.updated_on AS t0_r8, search_requirements.details AS t0_r9, status.id AS t1_r0, status.name AS t1_r1, search_requirements.id AS t0_r0, search_requirements.client AS t0_r1, search_requirements.sector AS t0_r2, search_requirements.date_of_enquiry AS t0_r3, search_requirements.position AS t0_r4, search_requirements.consultant AS t0_r5 FROM search_requirements LEFT OUTER JOIN status ON status.search_requirement_id = search_requirements.id || It should actually be looking for status.id. Am I missing something? Thanks, Vamsee.
Vamsee Kanakala wrote:> Alex Young wrote: > >> def list >> @things = Thing.find(params[:id], :include=>:status) >> end >> >> and >> >> <% @things.each do |thing| %> >> Name: <%= thing.name %> >> Status: <%= thing.status.name %> >> <% end %> >> >> > Thanks Alex, > > But in my case it''s not so straight forward - I want to > do pagination too, > so I did something like this (borrowed from > http://wiki.rubyonrails.com/rails/pages/PaginationHelper): > > def list > @requirement_pages = Paginator.new self, SearchRequirement.count, > 10, @params[''page''] > @requirements = SearchRequirement.find(:all, :include => :status) > end > > And says something like this: > > ERROR: column status.search_requirement_id does not exist > : SELECT search_requirements.location AS t0_r6, > search_requirements.status_id AS t0_r7, search_requirements.updated_on > AS t0_r8, search_requirements.details AS t0_r9, status.id AS t1_r0, > status.name AS t1_r1, search_requirements.id AS t0_r0, > search_requirements.client AS t0_r1, search_requirements.sector AS > t0_r2, search_requirements.date_of_enquiry AS t0_r3, > search_requirements.position AS t0_r4, search_requirements.consultant AS > t0_r5 FROM search_requirements LEFT OUTER JOIN status ON > status.search_requirement_id = search_requirements.id > || > It should actually be looking for status.id. Am I missing something? >Possibly you didn''t declare properly how are related :status and SearchRequirement. From your post I suggest that SearchRequirement has_one Status and status.id is also a reference to SearchRequirements.id. If so, declare has_one :status, :foreign_key => ''id'' in SearchRequirement model class. Though, it''s hard to say more w/o your schema part and model declarations :)
if i am understanding the problem, i think you need an associations between your models that corresponds to your table relationships see "Eager loading of associations" at http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html for why/when you should use :include class SearchRequirement < ActiveRecord::Base # search_requirements.status_id -> status.id belongs_to :status ... end # table "status" class Status < ActiveRecord::Base has_many :search_requirements ... end class SearchRequirementsController < ActiveController::Base ... def list @requirement_pages = Paginator.new self, SearchRequirement.count, 10, @params[''page''] @requirements = SearchRequirement.find(:all, :include => :status) end ... end On 9/30/05, Vamsee Kanakala <vamlists-hi6Y0CQ0nG0@public.gmane.org> wrote:> Alex Young wrote: > > >def list > > @things = Thing.find(params[:id], :include=>:status) > >end > > > >and > > > ><% @things.each do |thing| %> > >Name: <%= thing.name %> > >Status: <%= thing.status.name %> > ><% end %> > > > > > Thanks Alex, > > But in my case it''s not so straight forward - I want to > do pagination too, > so I did something like this (borrowed from > http://wiki.rubyonrails.com/rails/pages/PaginationHelper): > > def list > @requirement_pages = Paginator.new self, SearchRequirement.count, > 10, @params[''page''] > @requirements = SearchRequirement.find(:all, :include => :status) > end > > And says something like this: > > ERROR: column status.search_requirement_id does not exist > : SELECT search_requirements.location AS t0_r6, > search_requirements.status_id AS t0_r7, > search_requirements.updated_on AS t0_r8, > search_requirements.details AS t0_r9, > status.id AS t1_r0, status.name AS t1_r1, > search_requirements.id AS t0_r0, > search_requirements.client AS t0_r1, > search_requirements.sector AS t0_r2, > search_requirements.date_of_enquiry AS t0_r3, > search_requirements.position AS t0_r4, > search_requirements.consultant AS t0_r5 > FROM search_requirements LEFT OUTER JOIN > status ON status.search_requirement_id = search_requirements.id > > || > It should actually be looking for status.id. Am I missing something? > > > Thanks, > Vamsee. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Chris Hall wrote:>if i am understanding the problem, i think you need an associations >between your models that corresponds to your table relationships > > >Thank you very much! That worked flawlessly! I declared the relationships the other way round. That brings me to my next doubt. Isn''t it a little unnatural way of looking at relationships? I would say each of my SearchRequirement has got one Status, instead of saying that a Status has many SearchRequirements. Even in the example, how can an Author belong to a Post? And a Post has an Author? Am I missing something? Thanks, Vamsee.
Vamsee Kanakala wrote:> Isn''t it a little unnatural way of looking at relationships? I would say > each of my SearchRequirement has got one Status, instead of saying that > a Status has many SearchRequirements. Even in the example, how can an > Author belong to a Post? And a Post has an Author? Am I missing something? >Yes, names has_one, belongs_to are a bit confusing, but consider following: "belongs_to" is maybe not the best rephrase of "references to", e.g. table "Posts" has foreign key "author_id" that references table "Authors" "has_one", "has_many" - are "is referenced from one/many" , e.g. Author has_many Posts, because there are many records in table "Posts" which reference to this Author via foreign key "posts.author_id". "User" has_one "UserProfile", because there could be at most one record in table UserProfiles, which references to table "Users" via foreign key "user_id". I think it can be more clearly illustrated with following PDL chunk: object type User { // has_one UserProfile[0..1] profile=join users.id to user_profiles.user_id; // has_many Post[0..n] posts = join users.id to posts.author_id; // belongs_to Group[1..1] primaryGroup = join users.primary_group_id to groups.id; } # has_and_belongs_to_many association { // ProjectUsers User[0..n] users=join projects.id to project_users.project_id, join project_users.user_id to users.id; Project[0..n] projects=join users.id to project_users.user_id, join project_users.project_id to projects.id; String[1..1] role = project_users.role VARCHAR(30); Date[1..1] involveDate = project_users.involve_date DATE; }