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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:23:15+00:00 2026-05-29T08:23:15+00:00

I have a problem when trying to create a membership for users based on

  • 0

I have a problem when trying to create a membership for users based on the name they input into the membership form – new.html.erb.

user.rb

  has_many :memberships, :dependent => :destroy
  has_many :groups, :through => :memberships

membership.rb

class Membership < ActiveRecord::Base
  attr_accessible :user_id, :group_id
  belongs_to :user
  belongs_to :group
end

group.rb

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

membership controller

  def create
      @group = Group.find_by_name(:group)
      @membership = current_user.memberships.build(:group_id => @group.group_id)
      if @membership.save
        flash[:notice] = "You have joined this group."
        redirect_to :back
      else
        flash[:error] = "Unable to join."
        redirect_to :back
      end
    end

membership – _form.html.erb

<%= form_for(@membership) do |f| %>
  ...
  #error validation
  ...

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

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

What I want it to do is find the group inputted if it exists, and create the table entry in the memberships table accordingly. Just not sure if what I’m doing is on the right track. Any suggestions?

  • 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-29T08:23:15+00:00Added an answer on May 29, 2026 at 8:23 am

    The reason your code isn’t working now is because of this line:

    @group = Group.find_by_name(:group)
    

    it should be something like (I don’t remember exactly sorry)

    @group = Group.find_by_name(params[:membership][:group])
    

    The error is getting called on the next line because @group is nil.

    But you should probably handle that type of logic in the model anyway with a virtual attribute or something.

    membership.rb

    def group_name
      if self.group
        @group_name ||= self.group.name
      end
    end
    
    def group_name=(group_name)
      @group_name = group_name
    
      self.group = Group.find_by_name(@group_name)
    end
    

    form

    <div class="field">
      <%= f.label :group_name, "Group" %><br />
      <%= f.text_field :group_name %>
    </div>
    

    controller

    def create
      @membership = current_user.memberships.build(params[:membership])
      if @membership.save
        flash[:notice] = "You have joined this group."
        redirect_to :back
      else
        flash[:error] = "Unable to join."
        redirect_to :back
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a problem trying to modify a form I myself have
I'm trying to create a simple form with sonata bundle. I have one problem
I'm trying to create a new SMS receive listner. I have googled the problem
I have a problem with my WPF program. I'm trying to create an object
I'm trying to create a vertical button in PHP, but I have problem with
The Code I have this problem, I am trying to create a simple rectangle
I have a problem. I'm trying to create an array of different class objects
we have a problem when trying to create events for a custom control in
I have a problem trying to figure out how to create a query... Let's
I've run into a problem while trying to create a generic ArrayAdapter. It probably

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.