js111
js111

Reputation: 1314

How to add data from multiple models to same view (Rails 3.2)

Im trying to figure out how to have data from multiple models show up on the same calendar in my view. Right now it's showing birthdays, but i would love to add anniversaries & holidays as well (which are currently just lists in view).. *stripped unnecessary formatting for clarity

https://github.com/watu/table_builder

users/show.html.erb

 <% if @user.friends.any? %>
    <h3>Upcoming Birthdays</h3>
    <div id="calendar">
        <h2 id="month">
         <%= link_to "<", :month => (@date.beginning_of_month-1).strftime("%Y-%m") %>
         <%=h @date.strftime("%B %Y") %>
         <%= link_to ">", :month => (@date.end_of_month+1).strftime("%Y-%m") %>
        </h2>
                    <%= calendar_for @friends, :year => @date.year, :month => @date.month do |calendar| %>
                     <%= calendar.head('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') %>
                      <%= calendar.day(:day_method => :dob) do |date, friends| %>
                       <%= date.day %>
                        <ul>
                            <% friends.each do |friend| %>
                            <li> <%= link_to h(friend.name), friends %><%= "\'s birthday"%></li>
                         <% end %>
                        </ul>
                    <% end %>
                <% end %>
    </div>
    <% end %>

    <% if @user.holidays.any? %>
    <h3>Upcoming Holidays</h3>
    <% @user.holidays.each do |hld| %>
      <td><%= hld.name %></td>
      <td><%= hld.date %></td>
    <% end %>
    </tbody>
    </table>

Models

class User < ActiveRecord::Base
  has_many :friends
  has_many :user_holidays
  has_many :holidays, :through => :user_holidays
  has_many :anniversaries



class Holiday < ActiveRecord::Base
  attr_accessible :name, :date
  has_many :user_holidays
  has_many :users, :through => :user_holidays

end


class UserHoliday < ActiveRecord::Base
  attr_accessible :holiday_id, :user_id
  belongs_to :user
  belongs_to :holiday
end


class Anniversary < ActiveRecord::Base
  belongs_to :user
  attr_accessible :anniversary_date, :spouse_name, :anniversaries, :interest_ids
  has_many :person_interests, :as => :person
  has_many :interests, :through => :person_interests
end

Upvotes: 0

Views: 1721

Answers (1)

Ron
Ron

Reputation: 1166

There's nothing about a view that keeps it from using data from other models. You just have to make sure that the controller action passes it to the view somehow. You're currently passing one @date object and one @user object; if you want other objects you should set those as well. I advise you to make those objects accessible via the @user object, which is to say that you should create instance methods for a User so that you can access them in a view

class User

  def birthdays
    ...
  end

  def anniversaries
    ...
  end

  def holidays
    ...
  end
end

And then in your view, you can simply call @user.birthdays etc, etc.

Upvotes: 1

Related Questions