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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:59:51+00:00 2026-06-05T12:59:51+00:00

In my app I have users, schools (groups), and courses. I am trying to

  • 0

In my app I have users, schools (groups), and courses. I am trying to set up a model whereby any one of these three models can send any other model a request, and give the request-receiver an option to accept or deny the request. The idea I had was to set up a polymorphic to polymorphic association, so rails would identify the request_sender and receiver’s id and type. My first question is, does it make sense to set up a poly to poly table to achieve this functionality, or should I be trying something different? If a poly to poly table makes sense I included my current controller and db table:

The database table I set up for requests is:

class CreateRequests < ActiveRecord::Migration
  def change
    create_table :requests do |t|

      t.belongs_to :request_sender, polymorphic: true
      t.belongs_to :requestable , polymorphic: true

      t.timestamps
    end

    add_index :requests, [:request_sender_id, :request_sender_type]
    add_index :requests, [:requestable_id, :requestable_type]
  end
end

this is my request controller, the problem I am having is defining methods to determine the class of both the request sender and receiver:

def new
  @request = @requestable.requests.new
end

def create
  @request = @requestable.requests.new(params[:request])
  if @request.save
    redirect_to @requestable, notice: "requested."
  else
    render :new
  end
end

private
def load_requestable
  klass = [School, User, Course].detect { |c| params["#{c.name.underscore}_id"] }
  @requestable = klass.find(params["#{klass.name.underscore}_id"])
end

any help or guidance is greatly appreciated as I am new to both programming and rails, and have very little clue what I am doing haha.

  • 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-05T12:59:53+00:00Added an answer on June 5, 2026 at 12:59 pm

    Sure, no reason why this can’t work.

    what you’ll need when creating a message is to send in the class_name and ID of the creator and receiver. So let’s say your form sends in:

    • receiver_class
    • receiver_id
    • sender_class
    • sender_id

    Then, in your MessagesController

    def create
      message = Message.new.tap do |m|
        m.sender   = params[:sender_class].classify.constantize.find(params[:sender_id])
        m.receiver = params[:receiver_class].classify.constantize.find(params[:receiver_id])
        m.message  = params[:message]
      end
      if message.save
        redirect_to root_path, notice: "Awesome!"
      else
        render :new
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails app I have user, school, and course models. I have set
I'm trying to create an iOS (iPhone specific) app where users have to authenticate
In my app I have a user model, school model, course model, and a
In my project (iPhone app) I have a nice webview to allow users to
I've an application in the market and many users have reported that the app
I have an app that allows users to upload large data files and then
I have an app where users can change a toggle value between true and
I have an app where users can sign up with Facebook, in which case
I have an app in which users can follow law firms I have 3
I have an app and Im saving the users uploaded images to Amazon S3.

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.