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

  • Home
  • SEARCH
  • 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 760553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:43:54+00:00 2026-05-14T15:43:54+00:00

I need to display a UI element (e.g. a star or checkmark) for employees

  • 0

I need to display a UI element (e.g. a star or checkmark) for employees that are ‘favorites’ of the current user (another employee).

The Employee model has the following relationship defined to support this:

  has_and_belongs_to_many :favorites, :class_name => "Employee", :join_table => "favorites",
    :association_foreign_key => "favorite_id", :foreign_key => "employee_id"

The favorites has two fields: employee_id, favorite_id.

If I were to write SQL, the following query would give me the results that I want:

SELECT  id, account, 
    IF(
    (
    SELECT  favorite_id
    FROM    favorites
    WHERE   favorite_id=p.id
    AND employee_id = ?
    ) IS NULL, FALSE, TRUE) isFavorite
FROM        employees

Where the ‘?’ would be replaced by the session[:user_id].

How do I represent the isFavorite scalar query in Rails?

Another approach would use a query like this:

SELECT  id, account, IF(favorite_id IS NULL, FALSE, TRUE) isFavorite
FROM        employees e
LEFT OUTER JOIN favorites f ON e.id=f.favorite_id
    AND employee_id = ?

Again, the ‘?’ is replaced by the session[:user_id] value.

I’ve had some success writing this in Rails:

ee=Employee.find(:all, :joins=>"LEFT OUTER JOIN favorites ON employees.id=favorites.favorite_id AND favorites.employee_id=1", :select=>"employees.*,favorites.favorite_id")

Unfortunately, when I try to make this query ‘dynamic’ by replacing the ‘1’ with a ‘?’, I get errors.

ee=Employee.find(:all, :joins=>["LEFT OUTER JOIN favorites ON employees.id=favorites.favorite_id AND favorites.employee_id=?",1], :select=>"employees.*,favorites.favorite_id")

Obviously, I have the syntax wrong, but can :joins expressions be ‘dynamic’? Is this a case for a Lambda expression?

I do hope to add other filters to this query and use it with will_paginate and acts_as_taggable_on, if that makes a difference.

edit

errors from trying to make :joins dynamic:

ActiveRecord::ConfigurationError: Association named 'LEFT OUTER JOIN favorites ON employees.id=favorites.favorite_id AND favorites.employee_id=?' was not found; perhaps you misspelled it?
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1906:in `build'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1911:in `build'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1910:in `each'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1910:in `build'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1830:in `initialize'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1789:in `new'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1789:in `add_joins!'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1686:in `construct_finder_sql'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1548:in `find_every'
    from /Users/craibuc/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:615:in `find'
  • 1 1 Answer
  • 2 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-14T15:43:54+00:00Added an answer on May 14, 2026 at 3:43 pm

    Try this:

    ee=Employee.all( 
      :select=>"employees.*,favorites.favorite_id",
      :joins=>"LEFT OUTER JOIN favorites AS favorites 
               ON employees.id=favorites.favorite_id AND
                  favorites.employee_id = #{session[:user_id]}")
    

    Or to be exact:

    joins = Employee.send(:sanitize_sql_array, 
                 ["LEFT OUTER JOIN favorites AS favorites 
                   ON employees.id=favorites.favorite_id AND
                      favorites.employee_id = ? ", session[:user_id]
                 ])
    
    ee=Employee.find(:all, 
      :select=>"employees.*,favorites.favorite_id",
      :joins=> joins )
    

    Second approach addresses the SQL injection issues.

    Edit 1

    To test these calls in irb do the following:

    Simulate the session object by creating hash:

    >> session = {:user_id => "1" }
    session = {:user_id => "1" }
    => {:user_id=>"1"}
    

    Execute the finder:

    >> ee=Employee.find(:all, 
          :select=>"employees.*,favorites.favorite_id",
          :joins=>"LEFT OUTER JOIN favorites AS favorites 
                   ON employees.id=favorites.favorite_id AND
                      favorites.employee_id = #{session[:user_id]}")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a display element according to whether the user is logged or not
I need to display a tooltip after some event(on another element) How can I
I need to display the parent from the first Element of input.xml Also I
I have the following HTML where I need the LI elements to vertically display
I need to display all the employees in one webpart. I created the gridview
I need to display a string on a simulation screen. For that I'm supposed
I need to write a clause into my xslt that says if an element
Need to display an element ( div ) ontop of webpage. During scroll the
I have a plist file that holds information I need to display in an
I need to display a company name so that the "main" part of the

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.