Afolabi Olaoluwa Akinwumi Afolabi Olaoluwa Akinwumi - 10 months ago 46
Ruby Question

Rails Mailer messed up

I implemented a RoR mailer with my Ruby on Rails Application and it worked well. But to my surprise, every time a mail is sent, other attributes from my database table is populated in my sent email which I do not want.

To have a diagramatic view of my problem, see this Mailer Preview image:

enter image description here


class Job < ActiveRecord::Base
def self.jobs_posted_12hrs_ago
where.not('stripeEmail' => nil).where.not('payola_sale_guid' => nil).where('created_at > ?', - 12.hours)


class JobNotifier < ApplicationMailer

def send_post_email(job)
@user = User.where(:email => true).all
emails = @user.collect(&:email).join("#{';'}")
@jobs = job
@job = job
mail(:to => emails, :bcc => User.pluck(:email).uniq, :subject => 'New job posted on FarFlungJobs')


email template/view(send_post_email.html.erb)

I did iterate over my scope and populated just Comapny Name, Job Title, and Date. but when the email is previewed/sent, other attributes in the database are populated with it.

<!DOCTYPE html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />

<h3> Hi, new job just posted on! </h3>

<p>Fresh Job</p>
<% jobs = Job.jobs_posted_12hrs_ago %>
<%= jobs.each do |job| %>
<section class="container">
<div class="row jobs">
<div class="col-md-4" style="font-size: 20px;"><strong><%= job.company_name %></strong></div>
<div class="col-md-4" style="font-size: 20px;"><%= link_to(job.title, job) %></div>
<div class="col-md-4 text-right" style="font-size: 20px; float: left;"><%= job.created_at.strftime('%d %B %y') %></div>
<%= render 'shared/two_breaks'%>
<% end %>

<p>Thanks for subscribing to FarFlungJobs once again. <%= jobs_url %> </p>


What can I do to make sure I only have just the specified attributes in my email template being sent, instead of it add all I dont want?

Answer Source

I think the problem is this line:

<%= jobs.each do |job| %>

Because of the <%= it will print out the result of the each statement, which is jobs.