James Gray
3/17/2006 2:26:00 PM
On Mar 17, 2006, at 5:32 AM, Ryan Williams wrote:
> I'm a ruby newbie and have what is probably a relatively simple
> problem
> I'm trying to solve (though I'm using rails, this is more a ruby
> question). Say that I created a hash:
>
> TASKS = {
> "homework" => "Do Your Homework",
> "chores" => "Your Have Chores",
> "exercise" => "Don't Forget to Exercise"
> }
>
> and then I had an Assignment model with:
>
> def self.find_assignments(person)
> find(:all, :conditions => ["person_id = ?", person .id])
Just FYI, Rails's dynamic finder methods are probably a little
cleaner way to do the above:
find_all_by_person_in(person.id)
> end
>
> Which I called in the controller like so:
>
> @current_assignments = Assignment.find_assignments(@person)
>
> So, @current_assignments works fine. Now, the problem. I want to
> display
> the tasks which have NOT yet been assigned (so that they can be
> assigned
> if desired). In the assignments table, I have a column called 'tasks'
> which correlates with the keys in the TASKS hash. So I just want to
> remove any key=>value pairs from TASKS that match with the 'tasks'
> column in @current_assignment and then loop through the resulting new
> hash in the view.
>
> I'm at a loss as how best to do this. It must be something so simple
> it's eluding me. Any suggestions?
How about something like:
todo = Hash[ *TASKS.reject do |name, desc|
@current_assignments.any? { |assign| assign.tasks == name }
end.flatten ]
Hope that helps.
James Edward Gray II