views:

51

answers:

1

I have several models:

  • Email
  • Letter
  • Call

All three belong to a model Campaign. And a Campaign has_many Contacts

I envision being able to see a schedule for Today by going to domain/schedule/today

What I'd like it to do would be to show all the Events (Email, Letter, Call) that have to happen today for each campaign.

I tried the following, but have some challenges in putting it into a controller versus into a View. There are many emails in campaign.

Email.days is the number of days from the contact.start_date that an email should be sent to the Contact.

ScheduleController < 

def index

   campaigns.each do |campaign| #goes through each campaign

      for contacts in campaign.contacts

         Email.find(:all).reject { |email| email.contact.start_date + email.days <= Date.now }


      end
   end

end
+1  A: 

You're actually asking the wrong question.. Controllers aren't linked to any model fundamentally, they really display whatever you want. You can have a FooController that displays all the Bars and a DogController that gives info about cats..

To solve your problem:

  1. You're not 'sharing' anything with your view for it to display.
  2. You're also putting the logic in the wrong place, and you're not actually fetching the campaigns from the database..

In your controller you need to fetch the data from the DB:

def index
  @campaigns = Campaign.all #share the list of campaigns with the view
end

In your view you display the campaign info..

<% for campaign in @campaigns %>
<!-- display info about the campaign -->

  <% for contacts in campaign.contacts %>
<!-- contact level info and so on.. -->

  <% end %>
<% end %>

There's much more to it, but hopefully this gets you pointed in the right direction.

radixhound
Yeah I relized that...what I was wondering in retrospect was whether I need to put all those loops in the View versus a way to put into the Controller.
Angela
I guess that hard part was getting into the loop around the actual email. I have a .find method...but I could have multiple emails...will the way I do it automatically loop through?
Angela
Yes, to add to this, I had created a controller called Schedule and an action called Today using def Today end and it said it expected an id...
Angela