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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:03:59+00:00 2026-06-06T15:03:59+00:00

Simple rails app: I have 2 models, user and intro [which is simply a

  • 0

Simple rails app: I have 2 models, user and intro [which is simply a message]. Each message has a sender (user) and receiver (user). Here’s the intro model (validations omitted):

class Intro < ActiveRecord::Base
  attr_accessible :content

  belongs_to :sender, class_name: "User"
  belongs_to :receiver, class_name: "User"

  default_scope order: 'intros.created_at DESC'
end

and now the user model:

class User < ActiveRecord::Base
    attr_accessible :name, :email, :password, :password_confirmation
    has_secure_password

    has_many :sent_intros, foreign_key: "sender_id", dependent: :destroy, class_name: "Intro"
    has_many :received_intros, foreign_key: "receiver_id", dependent: :destroy, class_name: "Intro"

    before_save { |user| user.email = email.downcase }
    before_save :create_remember_token

    private

        def create_remember_token
            self.remember_token = SecureRandom.urlsafe_base64
        end
end

The app currently lets the current user submit an intro into a form and associate with that message (a home page shows sent_intros). However, I could use some help in the intros_controller/create method when it comes to the received_intros function. How do I let an intro that is created by the current user be associated with (i.e. sent to) another specific user so that I can route it to a recipient’s inbox? Thank you.

class IntrosController < ApplicationController
  before_filter :signed_in_user

  def create
    @sent_intro = current_user.sent_intros.build(params[:intro])
    if @sent_intro.save
        flash[:success] = "Intro sent!"
        redirect_to root_path
    else
        render 'static_pages/home'
    end
  end

  def index
  end

  def destroy
  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-06-06T15:04:00+00:00Added an answer on June 6, 2026 at 3:04 pm

    It doesn’t look like you’re allowing the current_user to assign a receiver to an intro they create? You need to have an input on your form that allows a user to set a valid receiver_id, and you need to add receiver_id to attr_accessible:

    class Intro < ActiveRecord::Base
      attr_accessible :content, :receiver_id 
    
      #Rest of your code
    end
    

    With that, when your intro is created, it will be properly associated with both a sender and a receiver. You would then be able to access a current_user’s received intros with the method current_user.received_intros

    You may want to add some validation to the Intro model to make sure both a receiver and a sender exist.

    EDIT: You can add the receiver_id field to your code in the comments like so:

    <!-- In your first view -->
    <% provide(:title, 'All users') %> 
    <h1>All users</h1> 
    <%= will_paginate %> 
    <ul class="users"> 
      <%= @users.each do |user| %> 
        <%= render user %> 
        <%= render 'shared/intro_form', :user => user %>  <!-- make sure you pass the user to user intro_form -->
      <% end %> 
    </ul> 
    <%= will_paginate %> 
    
    <!-- shared/intro_form -->
    <%= form_for(@sent_intro) do |f| %> 
      <%= render 'shared/error_messages', object: f.object %> 
      <div class="field">  
        <%= f.text_area :content, placeholder: "Shoot them an intro..." %> 
      </div> 
      <%= observe_field :intro_content, :frequency => 1, :function => "$('intro_content').value.length" %> 
      <%= f.hidden_field :receiver_id, :value => user.id %> <!-- Add this to pass the right receiver_id to the controller -->
      <%= f.submit "Send intro", class: "btn btn-large btn-primary" %> 
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sitting here with a simple rails 3 app in which I have a simple
I have a simple model setup in my Ruby on Rails app. (User {name,
I currently have a Rails 3.0.10 app and I made two models with simple
for example a have simple rails app with two models, users and books. now
I have a simple Rails 3 blog app that needs an admin user so
In my rails app I have user, school, and course models. I have set
I have a really simple Rails app. Basically an article with comments. I want
I have a very simple Rails app with a very simple relational database: Category
I've got a very simple Rails 3 app with a User model run by
I have those models User, Developer, App, Permission A User can have Default Permision

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.