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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:14:12+00:00 2026-05-21T11:14:12+00:00

I am using rails 2.3. How do I validate a belongs_to association field that

  • 0

I am using rails 2.3.

How do I validate a belongs_to association field that is entered using a textbox? e.g. a blog post belongs_to a category but it is not entered using a select, but as the category name (let’s assume unique) in a textbox.

Possible errors are: wrong format, no such category in DB.

If I assign a string directly to the association it errors out with an AssociationTypeMismatch so I can’t assign to the same attribute and then use a custom before_validate method.

If I do the conversion work in the controller and add to the model’s errors collection, it is still saved (I think the errors collection is cleared before the validation is started).

How should I do this yet also use the standard error collection?

  • 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-21T11:14:13+00:00Added an answer on May 21, 2026 at 11:14 am

    You might want to consider using a dummy text-box with javascript autocomplete functionality. Place a hidden field in the form for the ID for that association. When the JS returns a list of matches, include the ID’s, and populate the hidden ID field when an item is selected.

    Otherwise, this might help. Note this is untested code.

    # Assumed that "Obj" is the class of your belongs_to association
    
    attr_accessor :obj_name
    def obj_name= value
      self.obj = Obj.find_by_name(params[:obj_name])  
    end
    
    validate :validate_obj
    def validate_obj
      # If there's no obj, report an error on our pseudo-field
      return if self.obj.present?
      return if self.obj_name.blank? # Allow empty object if blank name was specified
      error.add(:obj_name, "does not appear to be a valid obj name")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Rails v2.1, lets say you have an action for a controller that is
I'm using rails, but doing all my Javascript unobtrusively using jQuery in application.js .
I'm using rails 2.3.4 When I execute rake db:test:prepare the id field created is
I'm using rails 2.3.5 with i18n. I's there a way to find all not
I am writing a testing script using Ruby and Rails. I have to validate
I am using Cucumber / Capybara with Rails 3 and am trying to validate
I've got a Rails 3 app (using Mongodb and Mongoid if that makes a
I'm using CarrierWave on my Rails 3 sample app. I want to validate the
How to validate corrupt image file while uploading using Rails if the corrupted jpg
I'm using form_remote_for() to do validation on a form's data so that my rails

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.