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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:25:50+00:00 2026-05-11T19:25:50+00:00

The business logic is this: Users are in a Boat through a join table,

  • 0

The business logic is this: Users are in a Boat through a join table, I guess let’s call that model a Ticket. But when a User instance wants to check who else is on the boat, there’s a condition that asks if that user has permission see everyone on the Boat, or just certain people on the Boat. If a User can see everyone, the normal deal is fine: some_user.boats.first.users returns all users with a ticket for that boat. But for some users, the only people that are on the boat (as far as they’re concerned) are people in, let’s say the dining room. So if User’s ticket is “tagged” (using an acts_as_taggable style system) with “Dining Room”, the only Users returned from some_user.boats.first.users should be Users with tickets tagged “Dining Room”.

Just for the record, I’m not trying to design something to be insane from the getgo – I’m trying to wedge this arbitrary grouping into a (mostly) existent system.
So we’ve got:

class User
  has_many :tickets
  has_many :boats, :through => :tickets
end

class Ticket
  belongs_to :user
  belongs_to :boat
end

class Boat
  has_many :tickets
  has_many :users, :through => :tickets
end

Initially, I thought that I could conditionally modify the virtual class like:

singleton = class << a_user_instance ; self ; end
singleton.class_eval(<<-code
  has_many :tickets, :include => :tags, :conditions => ['tags.id in (?)', [#{tag_ids.to_s(:db)}]]
code
)

That gets all the way down to generating the SQL, but when generated, it generates SQL ending in:

LEFT OUTER JOIN "tags" ON ("tags"."id" = "taggings"."tag_id") WHERE ("tickets"._id = 1069416589 AND (tags.id in (5001,4502)))

I’ve tried digging around the ActiveRecord code, but I can’t find anywhere that would prefix that ‘id’ in the SQL above with an underscore. I know that associations are loaded when an ActiveRecord class is loaded, and I’d assume the same with a singleton class. shrug.

I also used an alias_method_chain like:

singleton = class << a_user_instance ; self ; end
singleton.class_eval(<<-code
  def tickets_with_tag_filtering
    tags = Tag.find(etc, etc)
    tickets_without_tag_filtering.scoped(:include => :tags, :conditions => {:'tags.id' => tags})
  end
  alias_method_chain :tickets, :tag_filtering
code
)

But while that approach produces the desired Tickets, any joins on those tickets use the conditions in the class, not the virtual class. some_user.boats.first.users returns all users.

Any type of comment will be appreciated, especially if I’m barking up the wrong tree with this approach. Thanks!

  • 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-11T19:25:51+00:00Added an answer on May 11, 2026 at 7:25 pm

    So a wild guess about your underscore issue is that Rails is generating the assocation code based on the context at the time of evaluation. Being in a singleton class could mess this up, like so:

    "#{owner.table_name}.#{association.class.name}_id = #{association.id}"
    

    You could get in there and define a class name property on your singleton class and see if that fixes the issue.

    On the whole I don’t recommend this. It creates behavior that is agonizing to track down and impossible to extend effectively. It creates a landmine in the codebase that will wound you or someone you love at a later time.

    Instead, consider using a named_scope declaration:

    class User
       has_many :taggings, :through => :tickets
    
       named_scope :visible_to, lambda { |looking_user|
          { :include => [ :tickets, :taggings ], 
            :conditions => [ "tickets.boat_id in (?) and taggings.ticket_id = tickets.id and taggings.tag_id in (?)", looking_user.boat_ids, looking_user.tag_ids ] 
          }
        }
    end
    

    While you may have to go back and edit some code, this is much more flexible in the ways it can be used:

    Boat.last.users.visible_to( current_user )
    

    It’s clear that a restriction is being placed on the find, and what the purpose of that restriction is. Because the conditions are dynamically calculated at runtime, you can deal with the next weird modification your client hits you with. Say some of their users have xray vision and clairvoyance:

    class User
       named_scope :visible_to, lambda { |looking_user|
          if looking_user.superhuman?
            {}
          else
            { :include => [ :tickets, :taggings ], 
              :conditions => [ "tickets.boat_id in (?) and taggings.ticket_id = tickets.id and taggings.tag_id in (?)", looking_user.boat_ids, looking_user.tag_ids ] 
            }
          end
        }
    end
    

    By returning an empty hash, you can effectively nullify the effect of the scope.

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

Sidebar

Ask A Question

Stats

  • Questions 238k
  • Answers 238k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would build a simple console application which iterates over… May 13, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer You could Use #{data[0]} instead of #{data} That's because split… May 13, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer You cannot set the maxRetryCount on a standard wsHttpBinding configuration.… May 13, 2026 at 6:52 am

Related Questions

I have an ASP.NET page that is interacting with a business class. I want
I have a website built in PHP 4 with a framework made by hand
I've done a fair amount of work on MVC on the web, and we're
Is there a way of offering the flexibility of Excel/Access development that end users

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.