deleting from hash question

R

Ryan Williams

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])
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?
 
J

James Edward Gray II

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
 
R

Ryan Williams

Just FYI, Rails's dynamic finder methods are probably a little
cleaner way to do the above:

find_all_by_person_in(person.id)

Yep. I totally flaked on that. Thanks!
How about something like:

todo = Hash[ *TASKS.reject do |name, desc|
@current_assignments.any? { |assign| assign.tasks == name }
end.flatten ]

Okay... this seems to work fine right up to the .flatten
I created a method in my model like so:

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

with "assigned" being @currently_assigned.

Rails is giving me:

NoMethodError in Admin#show

undefined method `flatten' for {"homework"=>"Do Your Homework!"}:Hash

At least it's correctly eleminating the already assigned tasks. Any idea
why I'm getting thrown this exception on a standard ruby method? It seem
to happen to me a lot.
 
J

James Edward Gray II

How about something like:

todo = Hash[ *TASKS.reject do |name, desc|
@current_assignments.any? { |assign| assign.tasks == name }
end.flatten ]

Okay... this seems to work fine right up to the .flatten
I created a method in my model like so:

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

with "assigned" being @currently_assigned.

Rails is giving me:

NoMethodError in Admin#show

undefined method `flatten' for {"homework"=>"Do Your Homework!"}:Hash

This is me being dumb and not realizing how clever Ruby is. You can
drop the flatten() and the Hash[] call:

TASKS.reject do |name, desc|
assigned.any? { |assign| assign.task == name }
end

Hope that helps.

James Edward Gray II
 
R

Ryan Williams

James said:
You can
drop the flatten() and the Hash[] call:

TASKS.reject do |name, desc|
assigned.any? { |assign| assign.task == name }
end

That did it, thanks! You've been a HUGE help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top