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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:29:45+00:00 2026-05-23T01:29:45+00:00

I have various roles defined by CanCan in my rails application. I recently implemented

  • 0

I have various roles defined by CanCan in my rails application. I recently implemented jQuery UI autocomplete and it works well. The problem is that when I submit the form, the find_by_name that occurs in the model can find records that do not belong to the current_user. I have the following in my view:

<strong><%= f.label :inventory_name, "Material" %></strong>
<%= f.text_field :inventory_name, :class => "inputbox" %><br>

And my jQuery looks like:

jQuery("input[id$=_inventory_name]").autocomplete({
  source: '/ajax/inventory',
  minLength: 2
});

Then I have an ajax controller that does the right thing:

def inventory
  inventory = Inventory.accessible_by(current_ability)
  if params[:term]
    like= "%".concat(params[:term].concat("%"))
    names = inventory.where("name LIKE ?", like)
  else
    names = inventory
  end

  list = names.map {|u| Hash[ :id => u.id, :label => u.name, :name => u.name]}
  render :json => list
end

But my model does not:

def inventory_name=(name)
  inventory = Inventory.find_by_name(name)
  if inventory
    self.inventory_id = inventory.id
  else
    errors[:inventory_name] << "Invalid name entered"
  end
end
def inventory_name
  Inventory.find(inventory_id).name if inventory_id
end

find_by_name will return the first match it finds regardless of who owns it. Ideally I’d like to change:

inventory = Inventory.find_by_name(name)

to

inventory = Inventory.accessible_by(current_ability).find_by_name(name)

But that violates the principles of MVC not to mention the model has no access to current_ability, current_user or the like. So my question is, how to I move this logic into my controller where I have access to these things? I can’t seem to wrap my head around it 🙁

  • 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-23T01:29:45+00:00Added an answer on May 23, 2026 at 1:29 am

    I ended up needing both a before and after filter:

    after_filter :save_new_project_inventory, :only => [:create]
    before_filter :update_project_inventory, :only => [:create, :update]
    

    …

    private
    def save_new_project_inventory
      # do this in an after filter so that
      # we will have a project.id
      @project_inventories.each { |key, value|
        value[:project_id] = @project.id
        ProjectInventory.create!(value)
      }
    end
    
    def update_project_inventory
      @project_inventories = {}
      params[:project][:project_inventories_attributes].each { |key, value|
        if value[:_destroy] != "false"
          project_inventory = ProjectInventory.find(value[:id])
          project_inventory.delete
        else
          if value[:id]
            project_inventory = ProjectInventory.find(value[:id])
            project_inventory.inventory_id = inventory_name(value[:inventory_name])
            project_inventory.save
          else
            if @project.nil?
              # can't save here because we need a project.id that we
              # won't have until after we save, so finish up in the
              # after filter
              @project_inventories[key] = {
                :inventory_id => inventory_name(value[:inventory_name])
              }
            else
              project_inventory = ProjectInventory.new
              project_inventory.inventory_id = inventory_name(value[:inventory_name])
              project_inventory.project_id = params[:id]
              project_inventory.save
            end
          end
        end
      }
      params[:project].delete(:project_inventories_attributes)
    end
    
    def inventory_name(name)
      inventories = Inventory.accessible_by(current_ability)
      inventory = inventories.find_by_name(name)
      if inventory
        inventory.id.to_s
      end
    end
    

    I’m not sure if it is the best way to do it, but it works.

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

Sidebar

Related Questions

I have a 3 tier application that requires security authorizations be placed on various
I have various pieces of generated HTML/JS that I load into Activity with WebView#loadDataWithBaseURL.
We have various related session variables for a complex page that has various things
i have various strings that look like that: $(gateway.jms.jndi.ic.url,0,tibjmsnaming, tcp)/topic/$(gateway.destination.prefix)$(gateway.StatusTopicName),$(gateway.jms.jndi.ic.username),$(gateway.jms.jndi.ic.password),abinding,tBinding i'm trying to figure
I have various Java Swing applications that are used by multiple users. My deployment
I have developed an application in ASP.NET 3.5 which utilizes the Membership and Roles
I have a web application that uses the asp.net membership and role providers to
We have various endeca rules set up through Rules Manager in our application which
I have 2 arraylists. One is principleList which contains integer values that denotes roles
Background I have a backoffice that manages information from various sources. 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.