0

I have been trying trying to use two attributes from two different models so they can be used with the calendar_for method provided by the table_builder plugin in the index view. I have been through the Rails guides for http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects and posts such as Ruby on Rails: How to join two tables however i must be doing something wrong.

My models are as follows:

Class Event < ActiveRecord::Base
belongs_to :user

and

class User < ActiveRecord::Base
User has_many :events

The different ways I have tried in the controller (not all at once) are:

@event.user.name
@users = User.joins(:event).where(:event => {:event_date => true})
@users = User.where(:event => :event_date)

Amoung others, my view looks like:

my view code:

<% calendar_for @users, :year => @date.year, :month => @date.month do |calendar| %>
<%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',   'Saturday') %>
<% calendar.day(:day_method => :created_at) do |date, users| %>
<%= date.day %>
  <ul>
    <% for user in users %>
      <li><%= link_to h(user.name), user %></li>
    <% end %>
  </ul>
<% end %>
<% end %>
</div>

I have tried changing the variables in the view accordingly however to no avail. I would like to show the users name and a link to the user on the specific day that their event is booked.

Community
  • 1
  • 1
Steve
  • 1
  • 1

1 Answers1

2

Two issues I see, though I'm not familiar with the calendar library.

First, make sure your query in the controller returns something useful. Of the three lines you gave us, the first doesn't even search, it calls methods on an undefined variable. The second is close to working, but you are searching for a date and matching it to true... How about:

@users = User.joins(:event).where('events.event_date is not null')

Furthermore, if you have a date range, you might include that in the search:

@users = User.joins(:event).where('events.event_date > ? and events.event_date < ?', start_date, end_date)

Next in the view, you aren't consistent with your variable naming. The controller sets up the @users variable, which you access once, but then later you are missing the @ in front of it, which is not the same thing. I don't know what the calendar part wants as input, but at least the for loop should be:

for user in @users

That said, for loops are not very rubyish. The ruby way is to use each:

@users.each do |user|
  ...
end

or even better, to make all of your links:

<ul>
<%= @users.collect {|user| content_tag(:li, link_to h(user.name), user).join } %>
</ul>

edit

Based on more information, I think you are starting at the wrong place. Let's start with events.

Event.joins(:users).where('events.event_date is not null')


<% calendar_for @events, :year => @date.year, :month => @date.month do |calendar| %>
  <%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',   'Saturday') %>
  <% calendar.day(:day_method => :event_date) do |date, events| %>
    <%= date.day %>
    <ul>
      <% for event in events %>
        <li><%= link_to h(event.user.name), event.user %></li>
      <% end %>
    </ul>
  <% end %>
<% end %>
DGM
  • 26,629
  • 7
  • 58
  • 79
  • I have replaced the controller variable with your first suggestion as I dont have a date range, which works ok. Now am getting the error in the index that 'association named event was not found; perhaps I mispelled it' Its throwing this error at the <% calendar_for @users... %> line in the view before it gets to the rubyish each loops. I am new to Rails and have been using the Railscasts for the calendar. – Steve May 04 '11 at 13:21
  • I have progressed however I just want to change the caledar.day(:day_method to be => event_date instead of :created_at which just shows just the day the user was created instead of the day the event is on. Which I thought I needed to join the two tables to get access to this date? – Steve May 04 '11 at 14:29
  • sounds like it calls the "day_method" on the object passed in to the calendar, inwhich case you need to pas in the event object to the calendar, not the user. – DGM May 04 '11 at 15:17
  • Updated answer to start with events rather than users – DGM May 04 '11 at 15:25
  • no joy, Rails errors out in the controlle with "undefined method `joins'" am I missing the scope in the model for this? because I cannot get it to work. – Steve May 04 '11 at 18:51
  • derp, typo. the joins method should be called on Event class. – DGM May 04 '11 at 20:11