Thanks a lot Michael for your explanation and efforts. :)

I had thought about going the way you propose but it seemed to me like
too much work (I think the total amount of columns I would need to
write code for is 32) for what I actually need. If there is an easy
way of making the association work both ways I surely would like to
know, however, while you were trying to help me I think I found a neat
solution for my current need, which is not having the association work
both ways but accessing the project information for a given worker.

The problem I'm trying to solve is a little bit more complicated than
I originally posted. I am not really dealing with ID values. The
Projects table has an ID and also a project code, which is also
supposed to be unique "when it has a value" (this is enforced at
application level). The project ID is not what is stored in the
'project' columns in the Worker record, it is the project code
instead.

The solution I have so far works quite well and uses virtual
attirbutes (below). The first virtual attribute is a list of all
project codes actually stored in the record. If a 'project*' column is
empty it does not provide a value to the list. The second one is the
projects information based on those project codes the first virtual
attribute provides.

class Worker < ActiveRecord::Base
  attr_reader :project_codes, :projects

  def project_codes
    self.attributes.delete_if {|key, value| key.downcase !~ /project/
|| value.blank? }.values.map{|v| v.strip}.uniq
  end

  def projects
    Project.find_all_by_code self.project_codes
  end
end

On Feb 23, 2:56 pm, Michael Pavling <[email protected]> wrote:
> On 23 February 2010 19:54, Michael Pavling <[email protected]> wrote:
>
> > whoa! that doesn't look very legible once posted.
> > If you can't make sense of my code snippet, let me know and I'll post
> > a Gist link.
>
> In fact, I might as well do that anyway:http://gist.github.com/312634

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to