<-- Begin overly-long response to simple problem -->
Hey, I was playing around with the example above, and found, at least in
my case, that you could shorten that to just one line and it works fine
as well, as so:
engineers = Department.find_by_name(“Engineering”).employees
That should work just as well, let us know. I just tested it with a
complaints = Operators.find_by_name(“John Q. Public”).opcomplaints
where opcomplaints belongs_to operators and operators has_many
opcomplaints (hopefully, in the real world, our operators will have
few complaints, but the boss wants to keep track of it anyway). Worked
like a charm, in any case. Good luck!
Keep in mind, also, that the find_by_whatever methods are dynamically
generated, so if you have a field in your table that keeps track of
which employees are going bald, and you call it ‘balding’, then you
could get your list of employees that are going bald by using:
You can even search multiple fields… let’s say you’re keeping track of
which employees are getting a little big around the midle, and you call
that field ‘pudgy’. Then you can use:
It goes without saying that the first record returned here would, of
course, be my own.
In any case, I’m sure you get the idea. Dynamic Finders are one of
the coolest Ruby features, I think. And of course, there’s little or
nothing I could find about them in the official API doc, I had to go
here to find out about them: http://www.billkatz.com/agile_web_rails
Anyway, enjoy your coding!
Chang Sau S. wrote:
If everything is set up ok, then you can get find department object,
then get the array of employee objects from the department object.
engineering = Department.find_by_name(“Engineering”)
engineers = engineering.employees