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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:56:19+00:00 2026-06-16T21:56:19+00:00

Trying to find the definitive answer on whether active record associations should be in

  • 0

Trying to find the definitive answer on whether active record associations should be in the list of attr_accessible attributes.

I’ve seen

class Foo

  attr_accessible :name
  attr_accessible :bars

  belongs_to :bar
end

also seen

  attr_accessible :bars_id

want to know the proper way to be able to do Foo.new(name: ‘name’ bar: barvar)

  • 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-16T21:56:21+00:00Added an answer on June 16, 2026 at 9:56 pm

    As often the definitive answer is: “It depends™”

    Only the attributes you want to mass-assign should be made accessible.

    So if you want or need to do…

    Foo.new(name: 'name', bar: barvar)
    

    …then you simply have to make bar accessible.

    In the end assign_attributes is called which does a simple send("#{attribute_name}=", attribute_value) after checking the accessibility of the attribute.

    Some coding style aspects:

    Often mass assignment happens when processing the param hash. At least that’s where the security problems are lurking. There you rarely have a Bar object but more often a bar_id.

    However if you work with model instances, most people prefer using the association methods (as @Andrew Nesbitt wrote) because that often has some advantages (automatic saving, automatic update of the association counterpart, cleaner code, …)

    So there are reasons to have one or the other or both.

    My personal opinion: One should not waste a lot of time on this topic since Rails 4.0 will have a better solution for parameter sanitizing. (See strong_parameters if you want it in Rails 3, too)

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

Sidebar

Related Questions

I'm trying to find a definitive answer to the old question should i use
I'm trying to find a definitive answer (e.g. Apple developer documentation) for the maximum
I have been going round and round trying to find a definitive answer. Basically,
I can't seem to find a definitive answer for this, I'm trying to do
I'm trying to find a definitive answer to if it possible to set the
I'm trying to find a definitive answer and can't, so I'm hoping someone might
While trying to find an answer to Android Jasper Reporting I found out that
I'm trying to find a definitive no, but have been unsuccessful. Ideally, I'd like
So I am trying to find the answer to this, but there seems to
I spent some time trying to research a definitive answer for this and couldn't

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.