Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:32:05+00:00 2026-06-01T20:32:05+00:00

I have this relationship where User can create a document(trip) and invite other users

  • 0

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:

  1. 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?
  2. 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:32:06+00:00Added an answer on June 1, 2026 at 8:32 pm

    First of all, you have has_many :trips called twice in your User model. I understand you have two different types of User–Trip relationships (one directly, and one through Group), but you can’t give both the same name, otherwise one will hide the other. Try defining your User model 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
    

    There’s also the problem that you’re searching the friend‘s list of groups for a Trip object. Try changing that line to:

    <% if !friend.all_trips.include?(@trip) %>
    

    Or without the new method, something like this should work:

    <% if !friend.groups.where(:trip_id => @trip.id).first %>
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-many relationship like this: A user has_many organizations through affiliations and
I have players and fixtures that are in a HABTM relationship. This works well
I have User object with a list of groups that it belongs to: public
I have two tables, users and groups . A user can belong to many
Please help me model this in SQLAlchemy. A user can create a question. A
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have a one-to-many relationship between two classes for this situation. I have a
In this situation I have two models, Comment and Score. The relationship is defined
I have a simple has_one/belongs_to relationship between two models. This is a new association

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.