On 8 August 2010 11:20, Nick P. [email protected] wrote:
Say I have created a ‘user’ model (storing a id, name and email), a
‘society’ model (imagine club/societies at a school so it would have a
society’s name and code) and another model joining every user to a club
(i.e. user.id = society.id) all stored in a database. How would I, given
a society’s id, generate an array of all the names and email address of
users belonging to it to then send an email on to each?
Let’s say you’ve set up your models like this:
class User < ActiveRecord::Base
has_many :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :society
end
class Society < ActiveRecord::Base
has_many :memberships
end
Then, if you have the id of a society, this is the kind of thing
you’re trying to get
@society = Society.find(id)
@memberships = @society.memberships
@users = @memberships.map{|m| m.user}
The Ruby #map method on an array goes through each item in the array,
runs the code you specify (in this case, m.user to return the user for
that membership), and puts the results into a new array. Thus you now
have an array of the users who are members of that society.
For brevity, you could collapse that down to:
@users = Society.find(id).memberships.map{|m| m.user}
However, as a nicer alternative, the ActiveRecord ‘has_many’ accepts a
:through parameter, that lets you ‘reach through’ associations
automatically. So, if you amended your Society class to:
class Society < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end
then you can just do:
@users = Society.find(id).users
and it should work.
Then, sending the emails. You’d probably set up a mailer that just
takes a User object as a parameter; something like:
class SocietyMailer < ActionMailer::Base
def notification_email(user)
recipients user.email
body {:user => user}
end
end
You then have a ‘user’ variable in the email template, which you can
use to customise the email.
With that in place, you can send an email to each member with something
like:
@users.each{|u| SocietyMailer.deliver_notification_email(u)}
For more on mailers, see the guide:
http://guides.rubyonrails.org/action_mailer_basics.html
and for the has_many… :through construction, see:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
Chris