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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:14:17+00:00 2026-06-15T02:14:17+00:00

I use acts-as-taggable-on gem to populate a user interests on a User model like

  • 0

I use acts-as-taggable-on gem to populate a user interests on a User model like this

# User.rb
acts_as_taggable
acts_as_taggable_on :interests

As I populate the interest_list array, I need to check that the given values matches against a constant array to make sure these are accepted values, something like this

VALID_INTERESTS = ["music","biking","hike"]
validates :interest_list, :inclusion => { :in => VALID_INTERESTS, :message => "%{value} is not a valid interest" }

The code above returns the following error

@user = User.new
@user.interest_list = ["music","biking"]
@user.save
=> false …. @messages={:interest_list=>["music, biking is not a valid interest"]}

I can see the inclusion doesn’t realize it should iterate over the array elements instead of s considering as a plain string but I’m not sure how to achieve this. Any idea?

  • 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-15T02:14:19+00:00Added an answer on June 15, 2026 at 2:14 am

    The standard inclusion validator will not work for this use case, since it checks that the attribute in question is a member of a given array. What you want is to check that every element of an array (the attribute) is a member of a given array.

    To do this you could create a custom validator, something like this:

    VALID_INTERESTS = ["music","biking","hike"]
    validate :validate_interests
    
    private
    
    def validate_interests
      if (invalid_interests = (interest_list - VALID_INTERESTS))
        invalid_interests.each do |interest|
          errors.add(:interest_list, interest + " is not a valid interest")
        end
      end
    end
    

    I’m getting the elements of interest_list not in VALID_INTERESTS by taking the difference of these two arrays.

    I haven’t actually tried this code so can’t guarantee it will work, but the solution I think will look something like this.

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

Sidebar

Related Questions

I use acts-as-taggable-on gem and would like to fetch 5 most used tags, how
I want to use acts-as-taggable-on gem for my rails app. (rails 3.0.10 and ruby
Am using below specified gem acts-as-taggable-on(2.2.2) will_paginate(3.0.2) rails(3.0.3) ruby 1.8.7 i tried to use
I use acts-as-taggable-on https://github.com/mbleigh/acts-as-taggable-on for tagging user's post, I want to find one user's
I've implemented the framework outlined in this post: How to use jquery-Tokeninput and Acts-as-taggable-on
I have a User model, and use an acts_as_authentic (from authlogic) on it. My
Sorry for stupid question. but do I need to use standart model validation, such
I have a need for a container that acts like the following. It has
I am using acts-as-taggable on. Two models: User and Posts. Posts belongs_to :user User
Use acts-as-taggable-on for tagging. Based previous questions in SO i read that i must

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.