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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:53:47+00:00 2026-05-24T15:53:47+00:00

I have a relationship table in a rails application called edit_privileges , in which

  • 0

I have a relationship table in a rails application called edit_privileges, in which the User is the “editor” and a number of other classes are “editable”. Let’s say that two of those classes are Message and Comment.

My EditPrivilege model uses the following code:

belongs_to :editor, :class_name => "User"
belongs_to :editable, :polymorphic => true

And User, of course

has_many :edit_privileges, :foreign_key => "editor_id"

In order to determine if a user has edit privileges for a certain model, I can’t do the normal query:

user.edit_privileges.find_by_editable_id(@message.id)

because if the user has edit privileges to edit a comment with the same id as @message, the query will return true with the wrong edit privilege record from the table.

So, I tried doing these options:

user.edit_privileges.find(:all, :conditions => ["editable_id = ? AND editable_type ?", @message.id, @message.class.to_s])
user.edit_privileges.where(:editable_id => @message.id, :editable_type => @message.class.to_s)

which works great at finding the right record, but returns an array instead of an object (an empty array [] if there is no edit privilege). This is especially problematic if I’m trying to create a method to destroy edit privileges, since you can’t pass .destroy on an array.

I figure appending .first to the two above solutions returns the first object and nil if the result of the query is an empty has, but is that really the best way to do it? Are there any problems with doing it this way? (like, instead of using dynamic attribute-based finders like find_by_editabe_id_and_editable_type)

  • 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-24T15:53:48+00:00Added an answer on May 24, 2026 at 3:53 pm

    Use find(:first, ...) instead of find(:all, ...) to get one record (note it might return nil while find will raise an RecordNotFound exception). So for your example:

    user.edit_privileges.find(:first, :conditions => { :editable_id => @message.id, :editable_type => @message.class.to_s })
    

    BTW, if you’re on more edge rails version (3.x), Model.where(...).first is the new syntax:

    user.edit_privileges.where(:editable_id => @message.id, :editable_type => @message.class.to_s).first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called BlogPost which has a 1-to-many relationship with the Comment
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have a table relationship which links one person to many relatives. so the
suppose i have a entity called USER and a relationship FRIENDSHIP exist between two
I have two tables in my rails application Categories and Projects. The relationship between
I have a has_many, :through relationship in my Rails database, and the :through table
I have the following table relationship in my database: Parent / \ Child1 Child2
I have db table with parent child relationship as: NodeId NodeName ParentId ------------------------------ 1
I have a one to many relationship between two tables. The many table contains
I have the following tables in my database that have a many-to-many relationship, which

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.