Hi all, I''ve a two simple table, resource ======resource.id resource.name resource.contact and project =====project.id project.name project.resource_id (id from resource table) I want to display list of project that only belong to particular resource. my proj.rhtml is like ====================. .. ... <h1>Projects</h1> <table border="1"> <tr> <td width="30%"><p align="center"><i><b>ID</b></i></td> <td width="20%"><p align="center"><i><b>Project name</b></i></td> </tr> <% @project.each do |projects| %> #####some condition to be written #### <tr> <td><%= link_to projects.id, :action => "show", :id => projects.id %></td> <td><%= projects.name %></td> </tr> <% end %> </table> ... .. . in controller i''ve written def proj @resources =Resource.find_all @project = Project.find_all end which is showing all records from project table, How can write condition so that it will display records for particular resource. Please help me out to solve this Thanks, --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
First of all, the condition you want to implement should be in the controller, not in the view. You can to use eager loading and a condition to only load the resource in question (most likely defined by a id value in the request params) and the associated projects in one SQL query with Active Record. then in your view you loop through those projects and show them it would go something like this: #Controller def proj @resource =Resource.find(params[:id], :include => :projects) end #proj.rhtml <h1>Projects of Resource: <%= @resource.name %></h1> <table border="1"> <tr> <td width="30%"><p align="center"><i><b>ID</b></i></td> <td width="20%"><p align="center"><i><b>Project name</b></i></td> </tr> <% @resource.projects.each do |project| %> <tr> <td><%= link_to project.id, :action => "show", :id => projects.id %></td> <td><%= project.name %></td> </tr> <% end %> </table> NOTE: of course i hope you have set up the correct associations in the models, which are nessessary for the controller stuff: class Resource < ActiveRecord::Base has_many :projects end class Project < ActiveRecord::Base belongs_to :resource end On 10 Apr., 14:10, "aveo" <rahu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I''ve a two simple table, > resource > ======> resource.id > resource.name > resource.contact > > and > > project > =====> project.id > project.name > project.resource_id (id from resource table) > > I want to display list of project that only belong to particular > resource. > > my proj.rhtml is like > ====================> . > .. > ... > <h1>Projects</h1> > <table border="1"> > <tr> > <td width="30%"><p align="center"><i><b>ID</b></i></td> > <td width="20%"><p align="center"><i><b>Project name</b></i></td> > </tr> > > <% @project.each do |projects| %> > #####some condition to be written #### > <tr> > <td><%= link_to projects.id, :action => "show", :id => projects.id > %></td> > <td><%= projects.name %></td> > </tr> > > <% end %> > </table> > ... > .. > . > in controller i''ve written > def proj > @resources =Resource.find_all > @project = Project.find_all > end > > which is showing all records from project table, How can write > condition so that it will display records for particular resource. > Please help me out to solve this > Thanks,--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten, Thank you for your response.That is really appreciated. I''ve done changes stated by you,> #Controller > def proj > @resource =Resource.find(params[:id], :include => :projects) > end > > #proj.rhtml > <h1>Projects of Resource: <%= @resource.name %></h1> > <table border="1"> > <tr> > <td width="30%"><p align="center"><i><b>ID</b></i></td> > <td width="20%"><p align="center"><i><b>Project name</b></i></td> > </tr> > <% @resource.projects.each do |project| %> > <tr> > <td><%= link_to project.id, :action => "show", :id => projects.id > %></td> > <td><%= project.name %></td> > </tr> > <% end %> > </table> > > NOTE: of course i hope you have set up the correct associations in the > models, which are nessessary for the controller stuff: > > class Resource < ActiveRecord::Base > has_many :projects > end > > class Project < ActiveRecord::Base > belongs_to :resource > end >for Projects of Resource: <%= @resource.name %>, it is taking correct name from database. But at <% @resource.projects.each do |project| %> it is showing error as "undefined local variable or method `projects'' for #<#<Class........." I am unable to sort out it, please help me out. Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
well that error is most likely the result of a missing association. Have you set up the has_many :projects association as explained in my previous post? without that, the .projects method of the resource instance won''t be available and result in this error. On 10 Apr., 15:18, "aveo" <rahu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thorsten, > Thank you for your response.That is really appreciated. > I''ve done changes stated by you, > > > > > > > #Controller > > def proj > > @resource =Resource.find(params[:id], :include => :projects) > > end > > > #proj.rhtml > > <h1>Projects of Resource: <%= @resource.name %></h1> > > <table border="1"> > > <tr> > > <td width="30%"><p align="center"><i><b>ID</b></i></td> > > <td width="20%"><p align="center"><i><b>Project name</b></i></td> > > </tr> > > <% @resource.projects.each do |project| %> > > <tr> > > <td><%= link_to project.id, :action => "show", :id => projects.id > > %></td> > > <td><%= project.name %></td> > > </tr> > > <% end %> > > </table> > > > NOTE: of course i hope you have set up the correct associations in the > > models, which are nessessary for the controller stuff: > > > class Resource < ActiveRecord::Base > > has_many :projects > > end > > > class Project < ActiveRecord::Base > > belongs_to :resource > > end > > for Projects of Resource: <%= @resource.name %>, it is taking correct > name from database. > But at <% @resource.projects.each do |project| %> it is showing error > as > "undefined local variable or method `projects'' for > #<#<Class........." > I am unable to sort out it, please help me out. > Thanks- Zitierten Text ausblenden - > > - Zitierten Text anzeigen ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten wrote:> well that error is most likely the result of a missing association. > Have you set up the has_many :projects association as explained in my > previous post? > without that, the .projects method of the resource instance won''t be > available and result in this error.Thorsten yes I''ve already setup association in model as class Resource < ActiveRecord::Base has_many :projects end class Project < ActiveRecord::Base belongs_to :resource end Still same error. Regards, -- 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 -~----------~----~----~----~------~----~------~--~---
Thorsten, It worked, thanks a lot same thing, <% @resource.projects.each do |project| %> That is all correct. That was great. Please do a favor for me, Please let me know about code, done in controller " @met =Met.find(params[:id], :include => :projects) " How this worked. Thanks again, Regards -- 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 -~----------~----~----~----~------~----~------~--~---
> > " @met =Met.find(params[:id], :include => :projects) "Hi all, now I''ve one more table training =====training.id training.name training.resource_id (id from resource table) I want to display list of projects and trainings that only belong to particular resource. in my project controller ############ def show @resources =Resources.find(params[:id], :include => :projects) end ########### So it is showing projects associated with resource, But how can show trainings associated with resource on same page. I was trying something like #### @train = Training.find(@params["id"]) #### But doesnot work. Do I need to create association for this? Please help me out. Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
yes you have to create an association fort this, just like with the porjects table/model and then in your controller you can do: @resource =Resources.find(params[:id], :include => [:projects, :trainings]) this will include the associated projects and trainings as well. an you can use in your view : @resource.trainings.each do |training| blbla end just like with projects. On 19 Apr., 11:27, Rahul Ha <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > " @met =Met.find(params[:id], :include => :projects) " > > Hi all, > now I''ve one more table > > training > =====> training.id > training.name > training.resource_id (id from resource table) > > I want to display list of projects and trainings that only belong to > particular > resource. > > in my project controller > > ############ > def show > @resources =Resources.find(params[:id], :include => :projects) > end > ########### > > So it is showing projects associated with resource, But how can show > trainings associated with resource on same page. > I was trying something like > #### > @train = Training.find(@params["id"]) > #### > But doesnot work. Do I need to create association for this? > Please help me out. > > Thanks > > -- > 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 -~----------~----~----~----~------~----~------~--~---