1

I have an application on which a user can create multiple profiles of three kinds - social, professional and special interest. I have three models that store information about these profile types for a user. I need to create a drop down select menu of profile names for navigating between the views of these different profiles. I know it can be done using collection_select to populate from one database table, but how can I populate from all three tables?

Also can it be populated on a drop down navigation bar? Profiles -> View my profiles -> dynamically populate all profiles?

<ul id="nav" class="dropdown dropdown-horizontal">
    <li><%= link_to("Profiles", new_profile_path()) %>
    <ul>
          <li><%= link_to("View my profiles") %>
              <ul>
                  <li> *some way to populate my list here* </li>
              </ul> 
          </li>
        </ul>
           .
           .
           .

Any help is appreciated. Thanks!

1 Answer 1

0

I would suggest that unless absolutely necessary, instead of having three separate models for the three different profile types, you can have one User model and a single Profile model and then create an association between them. It will lead to cleaner design.

If it is absolutely necessary that you have three different Profile models then you can populate the drop down like this.

<select id="something">
  <%for profile in @social_profiles%>
    <option value="<%=profile.id%>"><%=profile.name%></option>
  <%end>
  <%for profile in @professional_profiles%>
    <option value="<%=profile.id%>><%=profile.name%></option>
  <%end%>
  .
  .
  .
</select>
2
  • I actually have no model called profile, the reason for this being, the three profiles have totally different set of fields/entries. So, I didn't see the need for a profile table, and related the User table directly with the three with the different profiles using one-to-many realtionship. My User model has_many socials and professionals. I tried replacing 'profile' in the above code with 'user', but it didnt seem to work. I'm new to rails, so my question may be a bit too naive. Sep 13, 2011 at 4:15
  • profile above is a loop variable and @social_profiles is an instance variable. You will have to query the Social model to get a list of all the Social records. @social_profiles = Social.all. Similarly for professional_profiles Sep 13, 2011 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.