Some help with a simple Rails problem?

Hi Guys. Im new to Rails, after developing in Java for about the last 4
years, because I want to develop better web based apps and find Ruby
would be better for this. Basically im learning as I’m going and what I
want to be able to do is:

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?

If you could help me solve this basic problem it would be fantastic as I
know seeing the code on how it is done will give me a resource do many
of the things I want to be able to do.

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:

and for the has_many… :through construction, see:

Chris

On Sun, Aug 8, 2010 at 7:51 AM, Chris M. [email protected] wrote:

Let’s say you’ve set up your models like this:

Nice response, Chris. Exemplary. Bravo!

Best regards,
Bill

Chris, I am absolutely blown away at your response. Here I was thinking
I would just get some guidance and you come back with an absolutely
flawless reply (really going above and beyond the call of duty). Thank
you SO much!! I will give it a go and I’m sure it will all work.

An absolutely brilliant gesture that shows me the Ruby community is the
one to be part of.

Thanks again,

Nick

On 8 August 2010 16:09, Nick P. [email protected] wrote:

Chris, I am absolutely blown away at your response. Here I was thinking
I would just get some guidance and you come back with an absolutely
flawless reply (really going above and beyond the call of duty). Thank
you SO much!! I will give it a go and I’m sure it will all work.

An absolutely brilliant gesture that shows me the Ruby community is the
one to be part of.

I would also suggest that you go to http://guides.rubyonrails.org/ and
read the guides there. I am sure you will find them very helpful.

Colin