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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:41:21+00:00 2026-06-09T20:41:21+00:00

So I have a Conversation model, which has_many messages . I’m trying to create

  • 0

So I have a Conversation model, which has_many messages. I’m trying to create a new message when I create a conversation. Here’s my ConversationsController:

class ConversationsController < ApplicationController
  before_filter :authenticate_user!
  def new
    recipient = User.find(params[:user])
    @conversation = Conversation.new(from: current_user, to: recipient)
    @conversation.messages.build(from: current_user, to: recipient)
  end

  def create
    @conversation = Conversation.create(params[:conversation])
    redirect_to @conversation
  end
end

And here’s my form (conversations/new.html.erb):

<%= form_for @conversation do |f| %>
  <%= f.fields_for :messages do |g| %>
    <%= g.label :subject %>
    <%= g.text_field :subject %>
    <%= g.label :content %>
    <%= g.text_field :content %>
  <% end %>
  <%= f.submit "Send" %>
<% end %>

The problem: when I submit the form, the conversation’s message gets saved, but the to and from fields that I specified as parameters in build are not saved (they are nil). However, the subject and content fields filled out in this form are saved just fine.

I’ve done a little bit of digging… if I do a puts on @conversation.messages in the new action, or in the new.html.erb, the message seems to have to and from. It’s only when the message reaches the create action do those fields disappear.

  • 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-09T20:41:23+00:00Added an answer on June 9, 2026 at 8:41 pm

    UPDATED:

    class ConversationsController < ApplicationController
      before_filter :authenticate_user!
      def new
        recipient = User.find(params[:user])
        @conversation = Conversation.new(to: recipient)
        @conversation.messages.build
      end
    
      def create
        @conversation = current_user.conversations.build(params[:conversation])
    
        # Set all the attributes for conversation and messages which
        # should not be left up to the user.
        @conversation.to = current_user
        @conversation.messages.each do |message|
          message.to = @conversation.to
          message.from = @conversation.from
        end
    
        redirect_to @conversation
      end
    end
    
    <%= form_for @conversation do |f| %>
      <%= f.hidden_field :recipient %>
      <%= f.fields_for :messages do |g| %>
        <%= g.label :subject %>
        <%= g.text_field :subject %>
        <%= g.label :content %>
        <%= g.text_field :content %>
      <% end %>
      <%= f.submit "Send" %>
    <% end %>
    

    You may still want to validate the recipient in your Conversation model.

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

Sidebar

Related Questions

I have a Conversation ActiveRecord model which is essentially the following class Conversation <
hi I have two rails model class Feedback < ActiveRecord::Base has_many :conversations, :dependent =>
I have a templated class Rect with conversion constructor which allows conversion between Rect
I have an app which essentially a conversation system (much like reddit). Where a
i have created the folloiwng view model class public class viewmodelclass { public IEnumerable<Question>
Hay, I'm using MPTT to create some tree-like data from a model which contains
Say I have a Item model, which has a boolean onshelf . I want
I have a function in my Teacher model which returns categories array. getCaterogies() {
I have a model which has one main table with 22 fields and 5
I have a collection for conversations: {_id: ..., from: userA, to: userB, message: Hello!,

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.