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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:09:35+00:00 2026-06-09T15:09:35+00:00

I have a simple nested form and I am setting a nested attribute with

  • 0

I have a simple nested form and I am setting a nested attribute with a hidden field:

<%= role_form.hidden_field :company_id, :value => session[:company_id] %>

The idea here is I am associating this nested model (a role based permissions system) to another model Company via the company_id set by the current session variable. My issue is that the user could send a request and create / update the role with an arbitrary company_id and gain access to another company’s account.

Can I force the nested model attributes to be this session value or perhaps a validation?

I was thinking for create:

@user = User.new(params[:user])
@user.roles.first.company_id = session[:company_id]

and for update I could do sort of the same thing.

As for the validation I tried:

accepts_nested_attributes_for :roles, :limit => 1, :allow_destroy => true , :reject_if => proc { |attributes| attributes['company_id'] != session[:company_id] }

but it looks like you can’t access the session info in the model.

Any one have an idea if I can do this either of these ways?

  • 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-09T15:09:36+00:00Added an answer on June 9, 2026 at 3:09 pm

    Rather than storing the company_id in the session, you should instead add a randomly generated token column to the company, and get the id by doing Company.find_by_token(session[:token]). If you look at how the current_user method in this Railscast on authentication, it’s the same idea.

    Edit:
    Sorry, I misunderstood your question. You should not have a hidden company_id field at all in your view. You should be setting it manually in your create method:

    @user = User.new(params[:user])
    @user.company_id = session[:company_id]
    

    And you can protect the company_id from ever being set from the user changing an input name by having company_id protected against mass assignment in the model:

    attr_protected :company_id
    

    See the rails guide on mass assignment protection for more information. Note: a more common solution is something along these lines:

    class ApplicationController < ActionController::Base
    
      protect_from_forgery
    
      def current_company
        @current_company ||= Company.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
      end
    end
    
    class User < ApplicationController
      def create
        @user = current_company.users.build(params[:user])
      end
    end
    

    UPDATE 2:

    So you’re creating a user and a role, and want to do separate validation on them, this should do what you want.

    role_params = params[:user].delete :role # Change to the appropriate symbol for your form
    @user = User.new(params[:user])
    role = @user.roles.build(role_params)
    role.company_id = session[:company_id]
    
    if(@user.save and role.user_id = @user.id and role.save) # Might want to just check for valid instead of trying to save
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Formtastic form with nested model as follow. <%= semantic_form_for @event
I have a simple structure in mongodb, with nested array. How can I update
I have simple php validation form that is halfway working. If you leave the
I have a nested form in a rails view that is called like this
I have a nested form in a Rails 3.1 app, using Cocoon gem. In
I have a nested simple XML structure that I load with PHP's simpleXML. Some
I have a simple UIWebView nested in a ViewController it is presented modally off
My nested form fields for friends will not show no matter what i do..have
I've got a relatively simple Rails app that takes a form with nested params
I have an ASP.NET MVC3 app that features a form with a nested-table input

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.