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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:59:42+00:00 2026-05-18T10:59:42+00:00

I have two tables: interests and Link_ui (this is for recording user and interest)

  • 0

I have two tables: interests and Link_ui (this is for recording user and interest)
I want to input the user id and show all interests name that user have.

In Link_ui controller:

 def output
    @interests = LinkUi.find_by_sql [ 'SELECT interests.name FROM link_uis, interests
    WHERE link_uis.interest_id = interests.id AND link_uis.user_id=? ', params['user_id'] ]

And input page:

<%= form_tag :action => 'output', :method => 'post' %>
  enter id.
  <%= text_field_tag ':user_id', '', 'size' => 30 %>

It comes out nothing, but I am sure there is matched data in database. And if I don’t input parameter just set link_uis.user_id = 1, it comes out:

your search are [#<LinkUi >, #<LinkUi >, #<LinkUi >, #<LinkUi >]

What’s wrong with this..

  • 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-18T10:59:42+00:00Added an answer on May 18, 2026 at 10:59 am

    Well, find_by_sql on a LinkUi model expects you to return columns from the link_uis table, whereas you’re selecting just interests.name. However, you are picking a bit of a fight with ActiveRecord, there. 🙂

    You usually want to avoid find_by_sql, and instead let ActiveRecord generate your SQL for you. Probably most important for your example are associations.

    The way I see it, you have a bunch of Users, and a bunch of Interests. Your LinkUis tie these two together (a LinkUi belongs to a User and an Interest). Feel free to correct me on this; this is your business logic as I gather from your example.

    These classes (whose names I’ve emphasized) are your models, defined in the app/models directory. The assocations (relationships) between them should be defined on those classes.


    Start of with a simple association in your User model:

    class User < ActiveRecord::Base
      has_many :link_uis
    end
    

    And in your Interest model:

    class Interest < ActiveRecord::Base
      has_many :link_uis
    end
    

    Then the LinkUi model that ties it together:

    class LinkUi < ActiveRecord::Base
      belongs_to :user
      belongs_to :interest
    end
    

    Now, given any User, you can get his/her LinkUis by simply saying user.link_uis.all, and for each LinkUi, you can get the Interest as link_ui.interest. You can tell ActiveRecord to try and fetch these two in one shot as efficiently as possible using :include, and get a list of Interest names using the standard Ruby collect method. It then becomes:

    user = User.find params['user_id']
    link_uis = user.link_uis.all(:include => :interest)
    interest_names = link_uis.collect { |link_ui| link_ui.interest.name }
    

    You can take it one step further; for any User, you can directly get his/her Interests. Once you’ve set up the above associations, you can fold two ‘steps’ into one, like this:

    class User < ActiveRecord::Base
      has_many :link_uis
      has_many :interests, :through => :link_uis
    end
    

    Which could turn the example into this one-liner:

    interest_names = User.find(params[:user_id]).interests.collect { |i| i.name }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables user table user_id | name | 1 | peter |
I have a SQL database with two tables like this: Users Id (PK) Name
We have two tables of interest in this query. First off we have a
I have two tables Person and Interest Person table structure Id | Name Interest
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables, a user table and a application table: User id username
I have two tables in my MySQL database, one is a library of all
I have a table with two fields of interest for this particular exercise: a
I have two mysql tables tblSharesRegistry, tblSharesAccount. I want to join both tables and
I have two tables, that relate via a one-to-many relationship i.e tableOne (1)----------(*) tableTwo

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.