...
..
.
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,
Thorsten,
Thank you for your response.That is really appreciated.
I’ve done changes stated by you,
Project name
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
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.
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
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.
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