Help with associations

Hi I have a site that has the following model declarations:

User:

has_and_belongs_to_many :categories
has_and_belongs_to_many :sub_categories

Item:

has_and_belongs_to_many :sub_categories
has_and_belongs_to_many :categories

Category:
has_many :sub_categories
has_and_belongs_to_many :items
has_and_belongs_to_many :users

SubCategory:
belongs_to :category
has_and_belongs_to_many :items
has_and_belongs_to_many :users

So when a user registers they select a number of categories and if they
want they can also select sub categories for more fine grained
interests. When a new item is added to the site, part of the item
registration process is to select the categories and sub-categories to
which the item belongs.

The bit that i would like advice/help/suggestions on is one a day i want
to run a cron job to go through the items added that day and to find all
the user-registered categories and sub-categories and find where they
intersect with the items categories and sub-cats and then produce a user
specific email telling that user about items they may be interested in.

Basically is it possible to iterate through the list of items and write
something like

item.categories.users and get a list of the users who registered
interest in this items categories?

Am i asking too much here? Will i just have to write some big iterative
function or is there an easier smarter approach?

Any help gratefully received.

best regards
caspar

IIRC, you can iterate only one level in depth, so you might have a
structure like:

items.categories.each do |category|
category.users.each do |user|
# do something here
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs