2

I basically followed the ROR guide, http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association, to create the relationship models as shown below.

Because of the through association, I figured that @user.trips would give you both the trips that the user created and the trips that belong to the user. However, when I do @user.trips.count in console, the result was only the number of trips that the users created; the trips that belonged to the user through the 'group' association was not counted.

Question: How do I get my view to display both the trips that the user created and the trips that the user belongs to through 'group'?

user/show.html.erb

<% unless @user.all_trips.empty? %>
  <% @user.all_trips.each do |trip| %>
     <!-- Content -->
  <% end %>
<% end %>

user.rb

class User < ActiveRecord::Base
  has_many :group_trips, :through => :groups,
                         :source   => :trip
  has_many :trips, :dependent => :destroy
  has_many :groups

  def all_trips
    self.trips | self.group_trips
  end


end

trip.rb

class Trip < ActiveRecord::Base
  belongs_to :user
  belongs_to :traveldeal
  has_many :groups

  has_many :users, :through => :groups
end

group.rb

class Group < ActiveRecord::Base
  belongs_to :trip
  belongs_to :user
end

Thanks!

Edit: Modified code per TSherif's partial solution. Edit 2: Fixed up the all_trips method. Everything appears to work for me at this point.

2
  • BTW, I think it's probably a better idea not to change around the code too much in your question. Now, if someone has the same problem you had, they won't be able to tell.
    – tsherif
    Apr 14, 2012 at 19:35
  • Will keep this in mind for next time; I figured it would be more helpful if people saw a working solution.
    – Huy
    Apr 14, 2012 at 19:57

1 Answer 1

2

Oh! I think I get what you're trying to do and why it's a problem. I was wondering why has_many :trips was called twice. But from what I understand, you have two different User-Trip relationships. These two can't have the same name, otherwise one will hide the other. Try something like this:

class User < ActiveRecord::Base
  has_many :group_trips, :through => :groups,
                         :class_name   => "Trip"
  has_many :trips, :dependent => :destroy
  has_many :groups

  def all_trips
    Trip.joins(:groups).where({:user_id => self.id} | {:groups => {:user_id => self.id}})
  end
end

Or if you're using an older version of Rails that doesn't have MetaWhere:

def all_trips
  Trip.joins(:groups).where("(trips.user_id = ?) OR (groups.user_id = ?)", self.id, self.id)
end
10
  • I haven't been sure that the Rails 3 joins method could take associations. Also, you seem to be passing an expression to the where method. Where can I read up on more information about these methods? api.rubyonrails.org and apidoc.com document the where method as: where(opts, *rest) So the only documentation I have is from the Rails 2.3 find method. Apr 14, 2012 at 14:54
  • 1
    The expression in the where clause uses MetaWhere syntax. You can get info about it here: erniemiller.org/projects/metawhere
    – tsherif
    Apr 14, 2012 at 15:15
  • Thanks. I'm very appreciative. Still, the Rails documentation would be a better place to find all this, instead of hunting around for stuff like this. :) Apr 14, 2012 at 15:59
  • Totally agree. I've been using Rails 3 for about a year now and just accidentally found out about this stuff last week watching a Railscast (which is a great resource, btw, if you aren't already using it).
    – tsherif
    Apr 14, 2012 at 16:10
  • 1
    Mmm... all that does is a set union between two separate queries. If you want to go that way, just do def all_trips; self.trips | self.group_trips; end. But are you saying that friend.all_trips.count produces something different from friend.all_trips.all.size? Can you post the SQL generated from the first query (from your execution log), so I can take a look at it. I think it should be working. Look for the one with SELECT COUNT(....
    – tsherif
    Apr 14, 2012 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.