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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:03:16+00:00 2026-05-13T19:03:16+00:00

I’m designing a form where you need to add a relation to another object.

  • 0

I’m designing a form where you need to add a relation to another object. Okay that’s normally fine, but what I am hung up on is a clean way to make it easy for the user to enter the object. There are multiple ways that the user could know how to specify the object (unique identifiers).

Let’s take the example of associating a user to a task. In this case, the models are laid out like this:

class User
   has_many :tasks
   # fields: phone_number, email, username
   validates_uniqueness_of :phone_number
   validates_uniqueness_of :email
   validates_uniqueness_of :username
   # other methods, validations, etc which are not important.
end

class Task
   belongs_to :user
   # other methods, validations, etc which are not important.
end

How would I write the controller and view form, if I want to be able to specify the user by username, email, or phone_number – I might know any one of these, and just one is enough to specify exactly the user that I want, in a clean way?

Currently my solution seems messy. I have a view similar to:

<% form_for @task do |f| %>
... Other stuff
User - choose one of the following ways: <br />
Username: <%= text_field_tag :user_name %> <br />
or phone number: <%= text_field_tag :user_phone %> <br />
or email: <%= text_field_tag :user_email %> <br /> 
... More other stuff
<% end %> 

I then handle these fields explicitly in the controller, finding the actual user based on which ones are filled in:

class TasksController 
  def create
    @task = Task.new(params[:task])
    if params[:user_name]
      @task.user = User.find_by_username(params[:user_name])
    elsif params[:user_phone]
      @task.user = User.find_by_phone_number(params[:user_phone])
    elsif params[:user_email]
      @task.user = User.find_by_email(params[:user_email])
    end
    if @task.save 
      redirect_to @task
    else 
      render :action => 'new'
    end
  end
end

This seems like it’s very specific, and there is a lot of code in my controllers, especially if I have lots of these on a form. Don’t even talk to me about when you need to dynamically add multiple users to a task – it gets even more crazy in the controller parsing everything out.

  • 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-13T19:03:16+00:00Added an answer on May 13, 2026 at 7:03 pm

    I think the most unobtrusive way of doing this is with a few AJAX calls.

    I’m thinking an observer on a text field that calls a remote function. That function should update your form with matching potential associations, that the user can select with a radio button or something.

    It would look like something like this:

    UserController:

    def select_user
      @user = case params[:query]
      when !/\w/ #phone no
        User.find_by_phone(params[:query])
      when /@/ # email
        User.find_by_email(params[:query])   
      else
        User.find_by_username(params[:query])
      end
    end
    

    views/select_user.rjs:

    page.replace_html :matched_user, :inline => <<"PARTIAL"
    <%=hidden_field_tag "task[user_id]", @user.id%>
    User: <%=@user%>
    PARTIAL
    

    task form

    <%form_for @task do |f|%>
      ...
      <%= text_field_tag :query %>
      <%= observe_field :query, {:controller => :users, :action => :select_user}%>
      <div id="matched_user" />
    <% end%>
    

    May not work as advertised. I made assumptions and haven’t tested it. But it should put you on the right track.

    When it comes to multiple users all that really needs to change is the hidden field tag and the rjs file. but not by much. There’s also nothing stopping you from using a more robust search mechanism.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I need to clean up various Word 'smart' characters in user input, including but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters

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.