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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:50:43+00:00 2026-05-15T00:50:43+00:00

I have 3 relevant tables. User, Orders, and Viewables The idea is that each

  • 0

I have 3 relevant tables.
User, Orders, and Viewables

The idea is that each User has got many Orders, but at the same time, each User can View specific other Orders that belong to other Users. So Viewables has the attributes of user_id and order_id.

Orders has a
:has_many :Users, :through => :viewables

Is it possible to do a find through an Order’s view?
So something like

@viewable_orders = Orders.find(:all, :conditions => [“Viewable.user_id=?”,1])

To get a list of Orders which are viewable by user_id=1. (This doesn’t work, else I won’t be asking. 🙁 )
The idea being that I can do something like a sidebar where the current user (the logged-in one) can view a list of other people’s orders that he can view.
For example

Three other Users who have some Orders that he can view should be eventually displayed like this:

  1. Jack (2)
    • Basic Order (registry_id: 1)
    • New Order (registry_id: 29)
  2. Amy (4)
    • Short Order (registry_id: 12)
  3. Jill (5)
    • Hardware Order (14)
    • Pink Order (17)
    • Software Order (76)

(The number in brackets are the respective user_id or registry_id)

So to find the list of all of the orders that the current user can find (assuming user_id of the current user is 1), would be found by doing

@viewable_orders = Viewable.find(:all, :conditions => ["user_id=?", 1])

And that would give me the collection of the above 6 registries. Now, the easiest way to do this, is for me to just have a list of

+ Jill's Hardware Order
+ Jill's Pink Order
+ Amy's Short Order
+ etc

But that gets ugly for long lists.

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-15T00:50:43+00:00Added an answer on May 15, 2026 at 12:50 am

    You should setup has_many :through association not only on orders model, but on users too. As you already have has_many :orders association for user, you can give your association another name, for example, viewable_orders:

    class User < ActiveRecord::Base
      has_many :orders
      has_many :viewables
      has_many :viewable_orders, :through => :viewables, :source => :order
    end
    
    class Viewable < ActiveRecord::Base
      belongs_to :user
      belongs_to :order
    end
    
    class Order < ActiveRecord::Base
      belongs_to :user
      has_many :viewables
      has_many :users, :through => :viewables
    end
    

    And then use can use user.viewable_orders to get all orders specific user can view

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

Sidebar

Related Questions

Not really a programming question, but relevant to many programmers... Let's say I have
I have two tables (well, two relevant for this question) : Bets (holds the
I have an XML document that I'm trying to style via CSS. A relevant
I have a strange problem with mod_rewrite, the rules that are relevant here are:
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you determined a maximum number of characters allowed in FCKEditor ? I seem

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.