I have this relationship where User can create a document(trip) and invite other users to a group that belongs to that document. My relationship indicates that “Group” has a user_id and trip_id column, so for every user I invite, a new Group record will be created in the database.
When I am inviting other users, I only want users who are NOT in the group to appear. Users who are already in the group should not show up, but my view still shows the users.
I’ve been playing around with <% if !friend.trips.include?(@trip)%>, but I can’t seem to get the correct view. The record is being created in the database correctly.
Also, when I am viewing groups/new.html.erb, this is the url http://localhost:3000/groups/new?id=2, where the id is the trip_id.
My question:
- Am I using restful convention? That is, should I be using the new method here (as is) or should I be using the index method instead?
- How do I iterate through each friend’s groups to make sure that none of the group’s trip_id is equivalent to @trip.id?
Thanks!
view (/groups/new.html.erb)
<% if !@friends.blank? %>
<% @friends.each do |friend| %>
<% if !friend.trips.include?(@trip)%>
<%= link_to groups_path(:user_id => friend.id, :trip_id => @trip.id),
:method => :post, :action => 'create' do %>
<div id="addfriend_totrip_button_groupsnew">add friend to trip</div>
<% end %>
<% end %>
<% end %>
<% end %>
groups_controller.rb
class GroupsController < ApplicationController
before_filter :authenticate, :only => [:update, :create, :destroy]
def new
@trip = Trip.find(params[:id])
@user = User.find(current_user)
@group = Group.new
@friends = @user.friends.all
end
def create
@trip = Trip.find(params[:trip_id])
@user = User.find(params[:user_id])
@group = Group.create(:user_id => @user.id, :trip_id => @trip.id)
if @group.save
flash[:success] = "Friend added to group."
redirect_to groups_path(:id => @trip.id)
else
flash[:error] = "Could not add friend."
redirect_to root_path
end
end
end
user.rb
class User < ActiveRecord::Base
has_many :trips, :through => :groups
has_many :trips, :dependent => :destroy
has_many :groups
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
First of all, you have
has_many :tripscalled twice in yourUsermodel. I understand you have two different types ofUser–Triprelationships (one directly, and one throughGroup), but you can’t give both the same name, otherwise one will hide the other. Try defining yourUsermodel like this:There’s also the problem that you’re searching the
friend‘s list ofgroupsfor aTripobject. Try changing that line to:Or without the new method, something like this should work:
I don’t see anything un-RESTful about your approach. RESTful in general means stateless. I.e. the only thing a response depends on is the HTTP method and the address. So as long as your not keeping state information in, say, the session, you should be following REST.