mercredi 11 janvier 2017

Ruby on Rails: Show all associated data on a view with a join table

In my RoR application I am trying to display information from associated tables on a view.

I have tables with data such as this:

Email
id      subject
1       This week's work
2       Presentation
3       Spreadsheets

Recipients
email_id       group_id
1              2
2              2
3              1
1              3

Groups
id        name
1         Managers
2         Employees
3         Directors

Contactgroups
group_id        contact_id
1               1
2               1
1               3
3               2

Contacts
id          email
1           Gary
2           Dave
3           Annie

What I am trying to do is display a list of groups that were sent an email as well as each group member. For example, email 1 "This week's work" was sent to groups 1 "Managers" and 3 "Directors", the "Managers" group consists of the contacts Gary and Annie and the "Directors" group consists of the contact "Dave". So on the show.html.erb page I want to display the email's details as well as the groups it was sent to and each of their contacts.

In order to do this, on the show.html.erb page I have the code:

<p>
  <strong>Groups Sent To:</strong></br>
  <% @recipients.each do |recipient| %>
        <%= recipient.group.name %>
        <% for i in 0..count_group_members(recipient.group.id)-1 do %>
            <%= group_member_name(recipient.group.id, i) %>
        <% end %></br>
  <% end %>
</p>

And in my emails_controller I have the code:

class EmailsController < ApplicationController
    helper_method :count_group_members, :group_member_name
    def index
        @useraccounts = Useraccount.where(user_id: session[:user_id])
        accountsarray = [0]
        @useraccounts.each do |f|
            accountsarray << f.account_id
        end
        # find all emails where the account_id is in the array - and so belongs to the user
        @emails = Email.where(account_id: [accountsarray])
    end
    def show
        @email = Email.find(params[:id])
        @account = Account.find_by_id(@email.account_id)
        @recipients = Recipient.where(email_id: @email.id)
    end
    def new
        @email = Email.new
        @email.recipients.build
        @useraccounts = Useraccount.where(user_id: session[:user_id])
    end
    def edit
        @email = Email.find(params[:id])
        @useraccounts = Useraccount.where(user_id: session[:user_id])
    end
    def create
        @email = Email.new(email_params)
        if @email.save
            redirect_to @email
        else
            render 'new'
        end
    end
    def update
        @email = Email.find(params[:id])
        if @email.update(email_params)
            redirect_to @email
        else
            render 'edit'
        end
    end
    def destroy
        @email = Email.find(params[:id])
        @email.destroy
        redirect_to emails_path
    end
    def count_group_members(group)
        Contactgroup.where(group_id: group).count
    end
    def group_member_name(group, i)
        contact = Contactgroup.where(group_id: group).offset(i).pluck(:contact_id)
        Contact.where(id: contact).pluck(:firstname)
    end
    private
    def email_params
        params.require(:email).permit(:subject, :message, :account_id, { contact_ids: [] }, { group_ids: [] })
    end
end

The problem with this is that on the show.html.erb view a user sees the information like this:

Groups Sent To:

Managers ["Gary", "Annie"] ["Annie"]

Directors ["Dave"]

When I want is to display as:

Groups Sent To:

Managers Gary, Annie

Directors Dave

Can someone please help me with this?

Aucun commentaire:

Enregistrer un commentaire