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?
On Mar 17, 2006, at 5:32 AM, Ryan W. wrote:
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)
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 G. II
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.
On Mar 17, 2006, at 1:03 PM, Ryan W. wrote:
Hash[ *TASKS.reject do |name, desc|
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 G. II
James G. wrote:
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!