# Some help with a simple Rails problem?

**URL:** <https://www.ruby-forum.com/t/some-help-with-a-simple-rails-problem/192916>\
**Category:** Rails\
**Created:** [August 8, 2010, 12:20pm UTC](https://www.ruby-forum.com/t/some-help-with-a-simple-rails-problem/192916 "2010-08-08T12:20:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![npenkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@npenkov](https://www.ruby-forum.com/u/npenkov)\
**Post date:** [August 8, 2010, 12:20pm UTC](https://www.ruby-forum.com/t/some-help-with-a-simple-rails-problem/192916/1 "2010-08-08T12:20:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![npenkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@npenkov](https://www.ruby-forum.com/u/npenkov)\
**Post date:** [August 8, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/some-help-with-a-simple-rails-problem/192916/2 "2010-08-08T14:53:21Z")

</div>

On 8 August 2010 11:20, Nick P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

> **[Action Mailer Basics — Ruby on Rails Guides](https://guides.rubyonrails.org/action_mailer_basics.html)**
>
> Action Mailer BasicsThis guide provides you with all you need to get started in sending emails from your application, and many internals of Action Mailer. It also covers how to test your mailers.After reading this guide, you will know: How to send...

and for the has\_many… :through construction, see:

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

Chris

---

<div class="post-metadata">

**Author:** ![npenkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@npenkov](https://www.ruby-forum.com/u/npenkov)\
**Post date:** [August 8, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/some-help-with-a-simple-rails-problem/192916/3 "2010-08-08T16:00:05Z")

</div>

On Sun, Aug 8, 2010 at 7:51 AM, Chris M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Nice response, Chris. Exemplary. Bravo!

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![npenkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@npenkov](https://www.ruby-forum.com/u/npenkov)\
**Post date:** [August 8, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/some-help-with-a-simple-rails-problem/192916/4 "2010-08-08T17:09:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![npenkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@npenkov](https://www.ruby-forum.com/u/npenkov)\
**Post date:** [August 8, 2010, 5:36pm UTC](https://www.ruby-forum.com/t/some-help-with-a-simple-rails-problem/192916/5 "2010-08-08T17:36:06Z")

</div>

On 8 August 2010 16:09, Nick P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://guides.rubyonrails.org/) and  
read the guides there. I am sure you will find them very helpful.

Colin
