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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:59:46+00:00 2026-06-15T12:59:46+00:00

I have a function which splits nested attributes into an array so they can

  • 0

I have a function which splits nested attributes into an array so they can be checked for uniqueness and then saves them. The problem is, i’m getting a Can't mass-assign protected attributes: _destroy error because the nested_form gem inserts a hidden_field to highlight which attributes should be deleted.

Here’s the *Posts_Contoller.rb* create function:

def create
   location_set = params[:post].delete(:locations_attributes) unless params[:post][:locations_attributes].blank?

   @post = current_blogger.blog_posts.new(params[:post])

   @post.locations = Location.find_or_initialize_location_set(location_set) unless location_set.nil?


 if @post.save
    redirect_to @post, notice: 'Blog post was successfully created.'
  else
    render action: "new"
  end
end

Here’s the function in location.rb:

def self.find_or_initialize_location_set(location_set)
  #create a locations array
   locations = []
         locations = locations.delete_if { |elem| elem.flatten.empty? }

   location_set.each do |key, location|
       locations << find_or_initialize_by_name(location)
   end

 locations

 end

And here’s the log:

 Started POST "/blog/posts" for 127.0.0.1 at 2012-12-03 17:31:54 +0000
 Processing by Blogit::PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"r74iCzC4tJgVI6FiCEH7XzfiTmaqKihF5JSs7Ow3MSI=", "post"=>{"title"=>"This
 is a post about Paris", "body"=>"This is a post about ParisThis is a post about Paris
 This is a post about ParisThis is a post about Paris", "tag_list"=>"", 
 "locations_attributes"=>{"0"=>{"_destroy"=>"false", "name"=>"Paris", "longitude"=>"2.3522219", "latitude"=>"48.856614"}, "1354555760002"=>{"_destroy"=>"false", "name"=>"Los Angeles", "longitude"=>"-118.5155901", "latitude"=>"3
 4.03563310000001"}}}, "_wysihtml5_mode"=>"1", "name"=>"losds", "legname"=>"Los Angeles",    "longitude"=>"-118.5155901", "latitud
 e"=>"34.03563310000001", "commit"=>"Submit"}

 User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
 ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON  "tags"."id" = "taggings"."tag_id" W
 HERE "taggings"."taggable_id" IS NULL AND "taggings"."taggable_type" = 'Blogit::Post' AND  (taggings.context = 'tags' AND taggings.tagger_id IS NULL)

 Location Load (0.1ms)  SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Paris' LIMIT 1
 LOGGER LOCATIONS ARE[#<Location id: 768, name: "Paris", latitude: #<BigDecimal:7f86aadf6230,'0.48856614E2',18(45)>, longitude:
 #<BigDecimal:7f86aadf5f60,'0.23522219E1',18(45)>, post_id: nil, created_at: "2012-12-01 17:27:33", updated_at: "2012-12-01 17
 :27:33", notes: nil>]
 Location Load (0.2ms)  SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Los Angeles' LIMIT 1
 Completed 500 Internal Server Error in 97ms

  ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: _destroy):
  app/models/location.rb:23:in `block in find_or_initialize_location_set'
  app/models/location.rb:21:in `each'
  app/models/location.rb:21:in `find_or_initialize_location_set'
  app/controllers/blogit/posts_controller.rb:79:in `create'

Any suggestions for how I can adapt this to include the delete function in the params as before but so I don’t get the Can't mass-assign protected attributes: _destroy error?

Thanks!

  • 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-15T12:59:47+00:00Added an answer on June 15, 2026 at 12:59 pm

    You could do something like:

    def self.find_or_initialize_location_set(location_set)
      #create a locations array
      locations = []
      locations = locations.delete_if { |elem| elem.flatten.empty? }
    
      location_set.each do |key, location|
        if location.delete(:_destroy) == "1"
          locations.delete_if {|elem| elem[:name] == location[:name]}
        else
          locations << find_or_initialize_by_name(location)   
        end
      end 
    
      locations 
    end
    

    Calling delete on the associated hash will modify the hash and return the value you’re deleting (or nil if not present).

    Or if you need the delete on the initialized location, you could do it there:

    def find_or_initialize_by_name_with_delete(location)
      if location.delete(:_destroy)
        do_delete
      end
    
      find_or_initialize_by_name(location)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do MySQL have any function by which I can split data of a column
Can I´m asking for advice. I have function which should return javascript object function
I have a function which zips files and downloads them to the users machine.
I have a function which repeatedly indexes into a dictionary with the same key.
I have a function which is suposed to return an array. But it is
I have successfully made a join function, which joins an array to a string
I have a function which reads a big text file,splits a part(from a given
Thanks in advance! I have a Function in SQL server 2008 which splits a
I have a function which grows an array when trying to add an element
I have custom function which return a table it accepts two varchars, it splits

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.