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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:03:38+00:00 2026-05-12T11:03:38+00:00

UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from

  • 0

UPDATE: Solved. Thanks BusyMark!
EDIT: This is revised based on the answer below from BushyMark. I think I made all the suggested changes correctly, but I still can’t get the post_type_id to save to the database when a user submits a message.

My app has a profile page. On the profile page, the owner of the page can type an update and hit “submit.” The update messages appear without the page reloading (i.e. Ajax).

It basically looks like your Twitter home page. BUT, there’s also a message “type” that our users select when typing an update. The message type is a pre-populated list. I’ve made it a model and load it as seed data in the app.

So: there is a profile model, a post model, and a post_type model.

profile has_many :posts
post belongs_to :post_type
post_type has_many :posts
The post model also has this: attr_accessible :message, :post_type_id

Routes.rb looks like this:
map.resources :users, :has_one => :profile, :has_many => :posts
map.resources :post_types

The posts controller has this method for creating the posts:

def create
  @post = current_user.profile.posts.build(:message => params[:message], :post_type_id => params[:post_type_id])
    if @post.save
      redirect_to user_profile_path
    else
      flash[:notice] = "Message failed to save."
      redirect_to user_profile_path
  end
end

The “show” view for the profile page where this all occurs looks like this:

<% form_tag(:controller => "posts", :action => "create") do %>
  <%= label_tag(:message, "Update your people") %><br />
 <%= text_area_tag(:message, nil, :size => "27x3") %>
 <div class="updateSubmit"><%= submit_tag("Update") %></div>
 <%= label_tag(:type_id, "Optional: Update type", :class => 'typeLabel') %>
    <%= collection_select(:post, :post_type_id, PostType.all, :id, :name, {:prompt => true}) %>
  <% end %>

    <% @profile.profile.posts.each do |c| %>
   <%=h c.message %></div>
   <p><%=h c.post_type %></p>
   <p>Posted <%=h time_ago_in_words(c.created_at) %> ago</p>
    <% end %>

That’s the background. Here’s the problem. With my current setup, when the user submits his message, the message gets sent to the posts database table, BUT the post_id does not.

My select tag is rendering like this:
<select id="post_post_type_id" name="post[post_type_id]"><option value="">Please select</option>

This is the output I get in the log when I submit a post:
Processing PostsController#create (for IP at 2009-09-03 03:03:08) [POST]

Parameters: {"message"=>"This is another test.", "commit"=>"Update", "action"=>"create", "authenticity_token"=>"redacted", "post"=>{"post_type_id"=>"3"}, "controller"=>"posts", "user_id"=>"1"}

User Load (0.3ms) SELECT * FROM "users" WHERE ("users"."id" = 1)

Profile Load (0.7ms) SELECT * FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1

Post Create (0.5ms) INSERT INTO "posts" ("message", "updated_at", "post_type_id", "profile_id", "created_at") VALUES('Hello', '2009-09-03 20:40:24', NULL, 1, '2009-09-03 20:40:24')

Here’s the relevant parts of my schema, updated per BushyMark’s comments:

  `create_table "post_types", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end`

  `create_table "posts", :force => true do |t|
    t.text     "message"
    t.integer  "profile_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "post_type_id"
  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-05-12T11:03:38+00:00Added an answer on May 12, 2026 at 11:03 am

    Couple things going on here I see so far:

    type is a reserved word in Rails. It is used for single table inheritance. If you change your column name to something like “post_type” it should help immensely in trying to solve random issues. I went through this a while back and was beating my brains out before I knew about STI.

    Also, you have a type_id in your post . . . but I didn’t see that your post :belongs_to :type . . . don’t forget that a model with the foreign key needs this declaration to use all the ActiveRecord associations.

    Hope this helps!

    Another Edit: I noticed that in your insert statement, it appears you have a “type” column? This will also cause issues when using relationships. Are you using a separate ‘type’ model? Can you possibly post your schema if any of the above statements don’t help?

    In ActiveRecord, a type column is reserved and will not display properly(back to my statement about Single Table Inheritance). At this point I strongly recommend getting rid of your type and type_id columns, and giving your Post a “post_type_id” column.

    Then once you create a separate post_type model that has_many :posts you will want to make sure your Post :belongs_to :post_type (because it has the foreign key). Once you do that, you will be able to call post_object.post_type and it will return the model relationship you are looking for.

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

Sidebar

Ask A Question

Stats

  • Questions 225k
  • Answers 225k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at Creating a self-signed certificate with ADT May 13, 2026 at 12:55 am
  • Editorial Team
    Editorial Team added an answer Yes, but you have to name the structure, so that… May 13, 2026 at 12:55 am
  • Editorial Team
    Editorial Team added an answer For rectangles (as NSRects) there is the Foundation function NSPointInRect():… May 13, 2026 at 12:55 am

Related Questions

I've got a ASP.NET website that is utilizing many aspects of the Enterprise Library
I have an array of dictionaries. I want to filter the array based on
Why does it (apparently) make a difference whether I pass null as an argument
First, the prior situation: We have this project with a one-click build script. It's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.