Hi folks! I’m about five weeks into Rails; this is my first question
after lurking for a little while.
I’m writing an app to help a printer verify that the information on
magazine covers is correct; this verification happens across several
organizations, and for several magazine titles. When a particular cover
is “stalled” in one status for two long, the app needs to figure out who
should get a warning message.
The way the db is designed right now is:
table “users” contains the users of the system; their logins, emails,
etc.
table “titles” contains magazine titles (“Celebrity Spoon Collectors”)
table “covers” contains individual covers (“Issue 2006.12.A3”)
table “notifications” describes which users get warnings for a title,
and which users get notified when all verifications are complete. It
contains user IDs, title IDs, and booleans for “warning” and
“verification.”
table “users” has_many :notifications
table “titles” has_many :covers and has_many :notifications
table “covers” belongs_to :title
table “notifications” belongs_to :title and belongs_to :user
So when the question arises:
“Given an overdue cover, who should get a notification about it?”
I could do this:
for each user in cover.title.notifications
[send email, etc…]
but “reaching” through three tables like that just seems kludge-y. From
“cover”, travel up to that cover’s title, then over to the notifications
for that title, then get the user from each matched object, etc…
Can you kind souls tell me if there’s a “preferred” way to handle this?
-
No, dot-script notation through three tables is fine?
-
Write a custom find method in model “notifications” so that you can
just ask that model for notifications, rather than doing things the hard
way in the controller?
Class Notification
def self.find_users_to_notify(cover, event)
[big ol’ “find_by_sql” left join find goes here]
end
end
Then my code could be like this:
for each user in Notification.find_users_to_notify( cover, “warning” )
- Something else that will cause me to slap my forehead?
Thanks for any advice! Right now, I’m using dot-script, but it seems…
painful.
Best regards,
John Young