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?
--
Posted via http://www.ruby-forum.com/.
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?
--
Posted via http://www.ruby-forum.com/.
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060317/1dfdb873/attachment.html
On 3/17/06, Ryan Williams <ryan@jimmyether.com> wrote:> 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?Haven''t tested, but maybe something like this? db_result.each {|row| TASKS.delete(row[''task''])} Justin
> 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) -- Posted via http://www.ruby-forum.com/.