Forum: Ruby on Rails deleting from hash question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2addfb9463ae86d58614733ea16b0682?d=identicon&s=25 Ryan Williams (jimmyether)
on 2006-03-17 12:11
I'm a ruby newbie and have what is probably a relatively simple problem
I'm trying to solve. 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])
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?
2addfb9463ae86d58614733ea16b0682?d=identicon&s=25 Ryan Williams (jimmyether)
on 2006-03-17 17:21
Sorry, perhaps I just didn't provide enough specific info. Trying again:

Since this is a multi-user system, there is no way to query the database
for unassigned tasks. I'm only inserting lines in the assignments table
when they currently exist for a particular person. If a "task" is not
assigned, then there is no entry for that task and person combo. So, all
I really have to go by is the static hash list of TASKS, which is a
complete list of all available tasks in the system which could
potentially be assigned to a person. And I need to compare the hash from
the database of "currently assigned" tasks to my static one of "all
available" so that I can remove them and be left with a list of
"currently unassigned tasks".

I want to have a method in the assignments model with
self.find_unassigned(person), but since I can't query the result, I need
to figure out how to compare my hash against the already existing
self.find_assignments() method. It's the comparing of the two (very
different) hashes that is perplexing me. I have this:


TASKS = {
    "homework" => "Do Your Homework",
    "chores" => "Your Have Chores",
    "exercise" => "Don't Forget to Exercise"
  }.freeze


and I have something like this that rail's "find" gives me on the
assignments table:

db_result = {
  "123" => {"id" => "123", "person_id" => "11", "task" => "homework"},
  "456" => {"id" => "456", "person_id" => "11", "task" => "chores"},
  "789" => {"id" => "789", "person_id" => "11", "task" => "exercise"},
}

So, using some elegant ruby code that I can't seem to find, I need to
dig down into the db result hash, looping through and comparing it to my
static hash, and then remove the key=>value pairs from my static hash in
the instances where has_value? matches.

Suggestions?
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-03-17 17:24
(Received via mailing list)
On Mar 17, 2006, at 3:11 AM, Ryan Williams wrote:

> TASKS = {
>     "homework" => "Do Your Homework",
>     "chores" => "Your Have Chores",
>     "exercise" => "Don't Forget to Exercise"
>   }


Here this should be what you want:

ezra-zygmuntowiczs-computer:~/sub ez$ irb
irb(main):001:0> TASKS = {
irb(main):002:1*     "homework" => "Do Your Homework",
irb(main):003:1*     "chores" => "Your Have Chores",
irb(main):004:1*     "exercise" => "Don't Forget to Exercise"
irb(main):005:1>   }
=> {"chores"=>"Your Have Chores", "homework"=>"Do Your Homework",
"exercise"=>"Don't Forget to Exercise"}
irb(main):006:0> TASKS.delete "chores"
=> "Your Have Chores"
irb(main):007:0> TASKS
=> {"homework"=>"Do Your Homework", "exercise"=>"Don't Forget to
Exercise"}


Cheers-
-Ezra
Ec30a6fd730b3c7788b59409ed568e7a?d=identicon&s=25 Justin Blake (Guest)
on 2006-03-17 18:11
(Received via mailing list)
On 3/17/06, Ryan Williams <ryan@jimmyether.com> wrote:
> available" so that I can remove them and be left with a list of
>     "homework" => "Do Your Homework",
>   "456" => {"id" => "456", "person_id" => "11", "task" => "chores"},
>   "789" => {"id" => "789", "person_id" => "11", "task" => "exercise"},
> }
>
> So, using some elegant ruby code that I can't seem to find, I need to
> dig down into the db result hash, looping through and comparing it to my
> static hash, and then remove the key=>value pairs from my static hash in
> the instances where has_value? matches.
>
> Suggestions?

Haven't tested, but maybe something like this?

db_result.each {|row| TASKS.delete(row['task'])}

Justin
2addfb9463ae86d58614733ea16b0682?d=identicon&s=25 Ryan Williams (jimmyether)
on 2006-03-17 22:55
> Haven't tested, but maybe something like this?
>
> db_result.each {|row| TASKS.delete(row['task'])}

That didn't quite work, but thanks! The answer ended up being to creat
the following method in my Attachment model:

  def self.get_unassigned(assigned)
    TASKS.reject do |name, desc|
    assigned.any? { |assign| assign.task == name }
    end
  end

where "assigned" is my @current_assignments hash passed by the
controller.

(credit to James Edward Gray II for the answer)
This topic is locked and can not be replied to.