I have three tables: Project, Group, Leader
Project has the following attributes:
:start_date
:end_date
:number_of_collaborators
:sector
Group has
:leader_id
:maximum_no_of_collaborators
:name
and Hosts has
:name
:contact_number
:region
In Project model:
belongs_to :group
In Group model:
belongs_to :leader
has_many :projects
In Leader model:
has_many :groups
I also have a search for to search for the leaders of available projects. In the search form there are 3 parameters:
:available_from
:available_until
:number_of_collaborators
In the search controller I'm now trying to pull up the details of available leaders. I've tried:
@projects = Project.where("start_date >= ? AND end_date >= ?",
Date.parse(params[:available_from]).strftime("%Y-%m-%d"),
Date.parse(params[:available_until]).strftime("%Y-%m-%d"))
I've filtered out the projects that have reached maximum capacity in the view by doing:
<% @projects.each do |project| %>
<% if ((project.group.capacity - project.number_of_collaborators) >0) %>
...code to display results...
<% end %>
<% end %>
This all seemed to work fine but it seems a very messy way to search for leaders. Another problem is that projects with room for more collaborators , that have the same leader, are being shown apart from each right now.
There has to be a more efficient way to get the leaders from the given search parameters.
I've tried (in the search controller):
@available_groups = []
@leaders = []
@projects.each do |project|
if ((project.group.capacity - params[:number_of_collaborators]) >0)
@available_groups << project
end
end
@available_groups.each do |grou|
@leaders << group.leader
end
But I get the error
Array can't be coerced into Fixnum
and I also think that it's just wrong/flawed to do things that way. I've been at this all day though and I can't wrap my head around the necessary logic.
To summarise: I'm trying to search for leaders given the search parameters :available_from, :available_until and :number_of_collaborators.
Any ideas?
Aucun commentaire:
Enregistrer un commentaire