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 7446111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:13:30+00:00 2026-05-29T12:13:30+00:00

I have the models User and Group, and the join table Membership which uses

  • 0

I have the models User and Group, and the join table Membership which uses has_many :through method. In my join table’s form, I want the user to input a valid group name that an administrator has created to become a member of that group.

I have gotten the case where a valid name is entered to work but now I need some validation if they input a blank text box, or that the inputted group name exists in the database, I’d like some nice error messages. I thought this would be possible through some validates method?

membership partial _form.html.erb

<%= form_for(@membership) do |f| %>
  <% if @membership.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@membership.errors.count, "error") %> prohibited this membership from being saved:</h2>

      <ul>
      <% @membership.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :group %><br />
    <%= f.text_field :group %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

membership.rb

class Membership < ActiveRecord::Base
  belongs_to :group
  belongs_to :user

  attr_accessible :user_id, :group_id

  validates_uniqueness_of :group_id, :message => "can be only joined once", :scope => 'user_id'
  validates_presence_of :group, :user
end

group.rb

class Group < ActiveRecord::Base

  has_many :memberships, :dependent => :destroy
  has_many :users, :through => :subscriptions

  validates :name, :presence => true, :uniqueness => true

  attr_accessible :name, :expiry
end

So need some direction as how the validation happens because the above validation in the membership and group models doesn’t work, I get the error for both empty text box or name not in the database…

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Edit: Added controller code below

  def create
    @group = Group.find_by_name(params[:membership][:group])
    @membership = current_user.memberships.build(:group_id => @group.id)

    respond_to do |format|
      if @membership.save
        format.html { redirect_to membership_url, :notice => 'Membership was successfully created.' }
        format.json { render :json => @membership, :status => :created, :location => @membership }
      else
        format.html { render :action => "new" }
        format.json { render :json=> @membership.errors, :status => :unprocessable_entity }
      end
    end
  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-05-29T12:13:32+00:00Added an answer on May 29, 2026 at 12:13 pm

    I solved this question by changing the text input box to a select field, and will add a password field to the model to solve my issue of unwanted users joining the group.

      def create
        @groups = Group.current
        @group = Group.find_by_name(params[:membership][:group])
        @membership = current_user.memberships.build(:group_id => @group.id)
    

    After which my model validation works now below

      validates_uniqueness_of :group_id, :message => "can be only be joined once", :scope => 'user_id'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships
I have two models, user and group. I also have a joining table groups_users.
I have two models -- User and Entry -- that are related through a
I have 3 models class User < ... belongs_to :language has_many :posts end class
I have a Class which models a User and another which models his country.
I have models that belong to some 'group' (Company class). I want to add
So I have 2 models. Users and Groups. Each group has a user as
I have hasMany Through table which is Chats table with Chat model and I'm
I want to represent clubs which have multiple members (each member can also join
How do I group named scopes? For example, I have two models, User and

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.