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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:38:53+00:00 2026-05-16T16:38:53+00:00

I have two tables: books (id, name, desc, instance_id) instances (id, domain) A user

  • 0

I have two tables:

books (id, name, desc, instance_id)
instances (id, domain)

A user should ONLY be able to see data that is assigned to their instance_id in records…

For the books, model, to accomplish this, I’m thinking about using a default scope.. Something like:

class Books < ActiveRecord::Base
    attr_accessible :name, :description
    belongs_to :user
    default_scope :order => 'books.created_at DESC'
        AND books.instance_id == current.user.instance_id 
end

Any thoughts on that idea? Also how can I write that 2nd to last line for Rails 3? ‘AND books.instance_id == current.user.instance_id’

Thanks

  • 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-16T16:38:54+00:00Added an answer on May 16, 2026 at 4:38 pm

    It’s not a good idea to access the current user inside the model. I would implement this as follows:

    class Instance < ActiveRecord::Base
      has_many :users
      has_many :books
    end
    
    class User < ActiveRecord::Base
      belongs_to :instance
      has_many :books, :order => "created_at DESC"
      has_many :instance_books, :through => :instance, :source => :books,
                       :order => "created_at DESC"
    end
    
    class Book < ActiveRecord::Base
      belongs_to  :user
      belongs_to  :instance
    end
    

    List of Books associated with the user instance:

    current_user.instance_books
    

    List of Books created by the user:

    current_user.books
    

    Creating a new book:

    current_user.books.create(:instance => current_user.instance, ..)
    

    Note:

    Your book creation syntax is wrong. The build method takes hash as parameter. You are passing two arguments instead of one.

    user.books.build(params[:book].merge(:instance => current_user.instance}))
    

    OR

    user.books.build(params[:book].merge(:instance_id => current_user.instance_id}))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables Users (name, email, password, instance_id, etc...) example: james bond, james@abc.com,
If i have two tables Books, CDs with corresponding models. I want to display
I have two tables, Book and Tag , and books are tagged using the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables: t1 ( date d, name c(20), debit n(7), credit n(7)
It's easier with an example. I have two tables: Books and Chapters. Both have
I have two tables like below, mysql> select * from Books ; +----+------+------------+----------+----------+ |
Let's say I have two tables, Books and Reviews. Reviews has a column, stars,
I have two tables books and tags which are in a many-to-many relationship via
Here's my simple SQL question... I have two tables: Books ------------------------------------------------------- | book_id |

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.