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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:40:56+00:00 2026-05-24T07:40:56+00:00

I’m trying to fill out an array with values from checkboxes. It works just

  • 0

I’m trying to fill out an array with values from checkboxes. It works just fine when creating a record, but fails validation when editing. The params look right, which is what really confuses me:

"record"=>{... "type_array"=>["accounting"], ...}

It looks the same as the params from creating a new record. The fields in New.html.erb and Edit.html.erb also use the same markup.

Edit.html.erb

<div class="field">
  <%= f.label :type_array, "What type of record?" %><br />
  <% ["accounting", "agriculture", "automotive"].each do |type| %>
    <%= check_box_tag 'record[type_array][]', type, (true if @record.type_list.include? type),
                      :id => type %>
    <%= label_tag type, type.titleize, :class => type %><br />
  <% end %>
</div>

Parts of Record.rb

validates :type_array, :presence => true
attr_accessor :type_array
attr_accessible :type_array
before_validation :set_type_list

private

def set_type_list
  self.type_list = type_array.join ',' if type_array.present?
end

Am I missing something? When I remove the type_array validation and fill out the form, it acts like type_array is empty. Somewhere along the line, it must be lost or something.

I appreciate any help.

(Sidenote: if anyone has a better way to do the list of checkboxes, let me know)

  • 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-24T07:40:57+00:00Added an answer on May 24, 2026 at 7:40 am

    Delete the line attr_accessor :type_array.

    This creates accessor methods to a new instance variable, not to the model attribute type_array, which means that @record.type_array now refers to that instance variable instead of the attribute.

    You almost never use attr_accessor or it’s siblings attr_reader and attr_writer in Rails because you want to deal with model attributes, not instance variables.

    Edit: You’re using type_array as a virtual attribute.

    class Record < ActiveRecord::Base
      validates :type_array, :presence => true
      attr_accessible :type_array
    
      def type_array=(val)
        self.type_list = val.join ','
      end
    
      def type_array
        self.type_list.split ','
      end
    
      def type_array_before_type_cast
        type_array
      end
    end
    

    For the reason why you need that last function definition, see this question.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

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.