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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:45:41+00:00 2026-05-19T06:45:41+00:00

I am confused with this. I have a company model and a message model.

  • 0

I am confused with this. I have a company model and a message model. Company has_many messages and messages belongs_to company. I am having problems with the data being saved to the proper company with this form is saved.

<%= form_for(@msg) do |f| %>
  <%= render 'errors', :object => f.object %>
    <ul class="fields">
      <li> 
        <%= select("msg", "company_id", Company.all.collect {|p| [ p.title, p.id ] }) %>
      </li>
    </ul>
    <ul class="fields">
      <li><%= f.label :content, "Send this company your message" %></li>
      <li><%= f.text_area :content %></li>
    </ul>
    <div id="actions">
      <%= f.submit "Send" %>
    </div>
<% end %>

every time this form is saved the company_id is null. My code for the controller to save is

def create
  @msg = current_user.messages.build(params[:msg])
  if @msg.save
    flash[:success] = "New message saved"
    redirect_to current_user
  else
    render current_user
  end
end

Nothing stands out to me as to why this is not saving, can someone guide me to the right direction?

Jeff

  • 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-19T06:45:43+00:00Added an answer on May 19, 2026 at 6:45 am

    In your controller, don’t you need to find the current company? Something like

    @company = Company.find(params[:company_id])
    @message = @Company.messages.create(params[:msg]) 
    

    would work. As far as mixing has_many with both users and companies, this thread on SO seems good. Rails Model has_many with multiple foreign_keys

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

Sidebar

Related Questions

I'm a bit confused on this. I have a data table structured like this:
Totally confused by this one... We have a WAMPServer installation set up, running a
I'm confused about this. Most of us have been told that there isn't any
I am very confused. I have this lambda expression: tvPatientPrecriptionsEntities.Sort((p1, p2) => p1.MedicationStartDate .Value
I have a question about my C++ homework. I am just confused about *this.
I've read a few posts on this but I'm still confused. I have this
I'm very confused and am hoping someone might have had this problem and be
A common issue I have is getting confused what $(this) is referring to. I
This is getting me quite confused: I have a small application that uses a
I have read many answers regarding this still i am getting confused if i

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.