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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:05:37+00:00 2026-05-22T16:05:37+00:00

In my app I have 3 models like that: class Person has_many :groups has_many

  • 0

In my app I have 3 models like that:

class Person
has_many :groups
has_many :group_memberships, :foreign_key => "member_id"
end

class Group_Membership
 belongs_to :member, :class_name => 'Person'
 belongs_to :group
end

class Group
belongs_to :person
has_many :group_memberships
has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"

What I need is to create a button in the group#show page, that allows the Person to ask for the group_membership. By suggestion from a previous question I made a def create_membership on my Group_Membership controller:

def create_membership
  @group = Group.find params[:id]
  if @group.group_memberships.create( :member_id => params[:member_id])
     redirect_to @group
  else
     render :action => 'show'
  end
end

So I moved on to my form, on the group#show view, and I did as follow:

<%= form_for :group_membership, @group_membership, :url => { :action => "create_membership" } do |f| %>
<%= submit_tag "Create" %>
<% end %>

But everytime I click on the button, it redirects me to the form to create a new Group, not Group_Membership. Any hints?
Thanks in advance.

########EDIT##########

Well, I kinda changed my code to something like this:

def create
@person = Person.find(current_person)
@group = Group.find(params[:group_id])
params[:group_membership] = {:member_id => @person.id, :group_id => @group.id}
@group_membership = Group_Membership.create(params[:group_membership])
if @group_membership.save
redirect_to @person
else
redirect_to @person
end
end

And my form is right now like that:

<% form_for(:group_membership, :url => group_memberships_path(:member_id => current_person, :group_id => @group.id)) do |f| %>
<%= submit_tag "Join!" %>

Now I’m able to actually create a group_membership model, but both id’s are added blank in database column member_id and group_id.

If I change the line @group_membership = Group_Membership.create(params[:group_membership]) to @group_membership = Group.Group_Membership.create(params[:group_membership]), I’m able to get group id saved. Same goes if I change to Person..

  • 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-22T16:05:38+00:00Added an answer on May 22, 2026 at 4:05 pm

    just to clean up a bit

    def create
      @group_membership = Group_Membership.create(:group => Group.find(params[:group_id]), :member => Person.find(current_person))
      if @group_membership.save
        redirect_to @person
      else
        redirect_to @person
      end
    end
    

    this code should work in rails 3 I’m not sure about earlier versions

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

Sidebar

Related Questions

In my Rails 2.3.2 app I have 2 models: class Post has_many :approved_comments, :class_name
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
I have a script that imports a models.py from an app, but it will
I have a ruby (1.9.3) on rails (3.1) app where: app/models/list.rb class List <
I have a parent model that has many child models, for example: App.Blog =
I have a class variable that I would like to set from an initilizer
I have these models: class App(models.Model): name = models.CharField(max_length=100) class ProjectA(models.Model): name = models.CharField(max_length=100)
I have a Rails app with the following (simplified) models: # member.rb class Member
I'm attempting to have inherited class templates, so that all my models have certain
I have a django project with 2 apps like this: ## tags app, models.py

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.