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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:37:47+00:00 2026-06-03T00:37:47+00:00

The excellent answer by Paz got me through a rough patch this week with

  • 0

The excellent answer by Paz got me through a rough patch this week with the autocomplete jQuery UI deal.

All that to say, How does one choose the correct json response if the you have two different "base" objects in your json response.

Simply put, I have a ruby instance variable made up of two models, Pitch and User. I want to autocomplete based on this instance variable. The json response is, say

[{"user":{"id":1,"name":"Blah Hugo"}},
{"user":{"id":3,"name":"Blaz Foobar"}},
{"pitch":{"id":2,"name":"Blasters On Business Apparel"}}]

The autocomplete javascript file looks like this NOW, and it works < snippet only >:

.data( "autocomplete" )._renderItem = function( ul, item ) {
  return jQuery( "<li></li>" )
  .data( "item.autocomplete", item )
  .append( "<a>" + (item.user ? item.user.name : item.pitch.name) + "</a>" )
  .appendTo( ul );
};

Again my problem is this ternary i had to use. item[0].name doesn’t work, nor did item.item[0].name Is there no way to get the generic ‘user’ or ‘pitch’ if you don’t know the name of either?

I’d have though one of them would. What says you?

  • 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-03T00:37:50+00:00Added an answer on June 3, 2026 at 12:37 am

    I don’t think there’s much wrong with what you have, but maybe a slight improvement would be:

    .data("autocomplete")._renderItem = function(ul, item) {
        item = item.user || item.pitch;
    
        return jQuery("<li></li>")
            .data("item.autocomplete", item)
            .append("<a>" + item.name + "</a>")
            .appendTo(ul);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is in reference to this (excellent) answer . He states that the best
Ok, I asked this question and got excellent code example as answer. The code
In my previous question I got an excellent answer that helped me detect where
I asked this question and got an excellent answer (thanks!). Part of the problem
Given this answer: https://stackoverflow.com/a/4263048 Which is excellent, how can we be sure that out
Reading this excellent answer about password hashing and wondering how to implement it: The
I find this answer and this wiki page to be excellent introductions to memoization
First of all, many thanks to Craig for the excellent answer below which I
I've found this excellent question / answer on how localize a string with the
From perldoc perlobj (quoted in this excellent answer ): my $fred = Critter->find(Fred); $fred->display(Height,

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.