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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:51:20+00:00 2026-06-13T09:51:20+00:00

I have ActiveRecord with a subclass and its associated with another ActiveRecord object. I

  • 0

I have ActiveRecord with a subclass and its associated with another ActiveRecord object.
I am able to create my object with nested attributes with a form with nested attributes no problem for a new object (following Ryan Bates rails cast – Thanks by the way :)). However when i do an update it fails to save the changes to either the main object or the related object when submitted

I have the following Activerecord classes and sub class.

class Room < ActiveRecord::Base

 attr_accessible :name, :type, room_headers_attributes

 has_many :room_headers, dependent: :destroy

 accepts_nested_attributes_for :room_headers , :allow_destroy => true

end

And the sub class is

class BigRoom < Room

end

And the related class is

class RoomHeader < ActiveRecord::Base

  attr_accessible :key, :room_id, :value

  belongs_to :room
end

In my room controller I created the nested objects. note that i’m using :type to specify the subclass type

def new
    @room = current_user.passes.build(params[:room])
    @room.type = params[:type]    

    3.times do
        room_header = @room.room_headers.build
      end

    ....
 end

 ....

def edit
  @room = Room.find(params[:id])
end

def update
    @room = Room.find(params[:id])

 if @room.update_attributes(params[:room])
 ...

The form used for creating and editing is the same

<%= form_for(@room) do |f| %>

<div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
</div>

<%= f.fields_for :room_headers do |builder| %>
   <%= render 'room_header_fields', f: builder %>
<% end %>

<div class="actions">

    <%= f.submit %>
  </div> 

<% end &>

And the _room_headers.html.erb partial is

<p class="fields">
  <%= f.label :key, "Key" %>
  <%= f.text_field :key %>
  <%= f.label :value, "Value" %>
  <%= f.text_field :value %>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove Header" %>
</p>

To recap on the problem. I can successfully create a new BigRoom. In the new form when i create the BigRoom and I can successfully set values for the RoomHeader class and these are all saved successfully.

However when i Edit the the record and submit changes for update, nothing is saved. Either for changes for the Bigroom attributes or to the associated RoomHeader records.

  • 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-13T09:51:22+00:00Added an answer on June 13, 2026 at 9:51 am

    Ok, nested attributes were a red herring. The problem is with STI

    The Rails form helper guide says you can’t rely on record identification with STI.

    In the form_for we need to coearce the ids to be the base type id otherwise the edit fails

    so

    <%= form_for(@room) do |f| %>
    

    should be

    <%= form_for(@room.becomes(Room) do |f| %>
    

    if you look at the difference in the html output

    the problem html would create ids like big_room_fieldname when in edit mode
    when using .becomes we get ids like room_fieldname. in whihc case it saves and updates ok

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

Sidebar

Related Questions

I'm looking for a solution to the following problem: I have an ActiveRecord entity
I have an ActiveRecord Appointment model, which includes topic and time attributes. I would
I have the following ActiveRecord query. It ran fine when the number of messages
I have a standard ActiveRecord model with the following: class MyModel < ActiveRecord::Base custom_method
I have multiple arrays of instances of ActiveRecord subclass Item that I need to
I have an ActiveRecord object, Corporation, and the only call in my project to
I have an Activerecord object called Foo : Foo.attribute_names.each do |attribute| puts Foo.find(:all)[0].method(attribute.to_sym).call end
I have an ActiveRecord model that has two database attributes, total and processing_fees .
I have an ActiveRecord object: @user = User.find_by_id(1) I want to access the name
Let's say I have an ActiveRecord with the following validation rules: public function rules()

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.