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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:27:22+00:00 2026-06-11T20:27:22+00:00

When I create a scaffold and I need to have a belongs_to relation to

  • 0

When I create a scaffold and I need to have a belongs_to relation to another model, I add a field called model_id (replacing model with that model’s name):

rails generate scaffold Grade user_id:integer subject_id:integer letter:string

Then in the above Grade model, I might add:

belongs_to :user
belongs_to :subject

Rails automatically adds user_id and subject_id to the list of attr_accessible fields. Do I do any harm by also adding :user and :subject to the list of attr_accessible fields so that I can mass assign using those as well?

  • 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-11T20:27:24+00:00Added an answer on June 11, 2026 at 8:27 pm

    attr_accessible is intended to protect against mass-assignment attacks that come from data that is externally sent to your application. In most cases you’re probably doing things like this in your create & update actions:

    @model = Model.new(params[:model])
    or
    @model.update_attributes(params[:model])
    

    You should ask yourself why you’d have one form that uses subject_id and another that uses subject. The only real harm here is inconsistency, which can actually be pretty detrimental to large projects. If you follow the convention that all forms will use the actual database column (subject_id), then you’ll save yourself some headache in the future when you can’t remember out why you did it two different ways.

    If you’re updating attributes through the console, you can either use update_attributes(params[:model], without_protection: true) or a gem I wrote called sudo_attributes which lets you do sudo_update_attributes(params[:model]).

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

Sidebar

Related Questions

I have created model, view and controller: $ rails generate scaffold Post name:string title:string
I have scaffold with 2 fields name:string, active:boolean.. This way scaffold create data... def
I have a simple controller that was created through a scaffold and has a
I have created model, controller and view with rails scaffold generator: rails g scaffold
I have an iOS project that sends a Person class with one attribute name
I have 2 controllers that I created using scaffold generator of rails. I wanted
I need to validate a model only for a certain action (:create). I know
I'm using Fuel 1.2.1 I have used scaffold to create my CRUD with the
I have a model class that has an AreDuesPaid property that I want only
When I add a new product using my scaffold create rails app, the following

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.