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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:41:44+00:00 2026-05-12T07:41:44+00:00

Say you have this structure: class House < ActiveRecord::Base has_many :rooms accepts_nested_attributes_for :rooms attr_accessible

  • 0

Say you have this structure:

class House < ActiveRecord::Base
  has_many :rooms
  accepts_nested_attributes_for :rooms
  attr_accessible :rooms_attributes
end

class Room < ActiveRecord::Base 
  has_one :tv
  accepts_nested_attributes_for :tv
  attr_accessible :tv_attributes
end

class Tv 
  belongs_to :user
  attr_accessible :manufacturer
  validates_presence_of :user
end

Notice that Tv’s user is not accessible on purpose. So you have a tripple-nested form that allows you to enter house, rooms, and tvs on one page.

Here’s the controller’s create method:

def create
  @house = House.new(params[:house])

  if @house.save
    # ... standard stuff
  else
    # ... standard stuff
  end
end

Question: How in the world would you populate user_id for each tv (it should come from current_user.id)? What’s the good practice?

Here’s the catch22 I see in this.

  1. Populate user_ids directly into params hash (they’re pretty deeply nested)
    • Save will fail because user_ids are not mass-assignable
  2. Populate user for every tv after #save is finished
    • Save will fail because user_id must be present
    • Even if we bypass the above, tvs will be without ids for a moment of time – sucks

Any decent way to do this?

  • 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-12T07:41:44+00:00Added an answer on May 12, 2026 at 7:41 am

    Anything wrong with this?

    def create
      @house = House.new(params[:house])
      @house.rooms.map {|room| room.tv }.each {|tv| tv.user = current_user }
      if @house.save
        # ... standard stuff
      else
        # ... standard stuff
      end
    end
    

    I haven’t tried this out, but it seems like the objects should be built and accessible at this point, even if not saved.

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

Sidebar

Related Questions

So, lets say that we have this structure: <div id='allTempLinkHolder'> <div class='tempLink'>Value</div> <div class='tempLink'>Value
let's say I have an HTML structure like this: <div class=first> <div class=sub-1> <div
Let's say I have this simple structure class FooDefinition { public FooDefinition Parent {
Say I have a structure like this: class AAA { BBB bb_member; double dbl_member;
Let's say I have this simple class structure: Vehicle |-- Bus `-- Car Bus
Say i have this this structure. MyApp ├── main.py └── package ├── __init__.py ├──
let's say I have a html structure like this -> (note: in my code
Let's say I have a first structure like this: typedef struct { int ivalue;
Lets say I have an array with a structure like this: $arr= Array( array(
Lets say have this immutable record type: public class Record { public Record(int x,

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.