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

  • Home
  • SEARCH
  • 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 7186051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:33:04+00:00 2026-05-28T18:33:04+00:00

I have fairly simple problem but I can not think of the simple solution.

  • 0

I have fairly simple problem but I can not think of the simple solution. I have 3 models.

class User < ActiveRecord::Base
  has_many :memberships
end

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :school
end

class School < ActiveRecord::Base
  has_many :memberships
end  

A User can create a membership for school(s). “membernumber” is given by school. So far so good.

Except one use case, where a membership record gets created by school without user_id value in memberships table.

In this case a record gets created with “membernumber”, “school_id” and empty “user_id” field.

What I want to do is, when a user tries to create his membership (using his “membernumber”) with a school, First I want to see if “user_id”, “membernumber”. school_id combination is unique if found (I used validates_uniqueness_of….).

If record not found than check a record/association with “membernumber” and school exists and it’s user_id field is empty. If that is the case then don’t create a new membership but just update it’s user_id field (with current_user.id) and return with a msg that a creation of new association is successful (as if its a new membership record).

How do I do this? Do I need to use any callbacks? How do I use callbacks in this case?
Any help is appreciated.

Thanks,

Atarangp

  • 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-28T18:33:05+00:00Added an answer on May 28, 2026 at 6:33 pm

    This is how I solved it.
    In model membership, added a method membership_exists and called it from membership’s create
    action.

    class Membership < ActiveRecord::Base
      belongs_to :school
      belongs_to :user
      #validations, etc.
    
      def Membership.membership_exists(membernumber, school_id)
         membership = Membership.where(:membernumber =>  membernumber, :school_id => school_id, :user_id => nil).first
         if membership.nil?
            return false
         else
            membership.update_attribute :user_id, user_id
            return true
         end
      end
    end
    

    And in Membership controller (in create action)

      def create
        @membership = Membership.new(params[:membership])
        @membership.user_id = current_user.id
    
        @flag = Membership.membership_exists(@membership.membernumber,@membership.school_id)
        if @flag == true
          flash[:notice] = "Membership creation is successful"
          redirect_to @membership
          return
        end
    
        respond_to do |format|
          if @membership.save
            format.html { redirect_to @membership, 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
    

    This works. But is it a good way to do it?

    Thanks,
    Atarangp.

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

Sidebar

Related Questions

I have what I think is a fairly common problem, but I cannot seem
Hi all I have a (probably) simple problem but I just can't figure out
I have a fairly simple class that I want to save to SQL Server
I am trying to do something I think should be fairly simple, but I
I have a fairly simple problem for someone familiar with js. I quite like
I have a fairly simple block of code. Sub Run(Name) on error resume next
I have a fairly simple python loop that calls a few functions, and writes
I have a fairly simple cell in a table with an inline style: <td
I have a fairly simple set of functionality for which I have multiple implementations,
I have a fairly simple Linq query (simplified code): dim x = From Product

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.