Topic: filter in view with rails_admin

Hi,  I am new to rails and I have a question to which I can't find the answer on any website.  I am using rails_admin for a easy admin application.  The thing is I have a table called owner and a table called computer which are liked to each other in the sense that an owner can have one or multiple computers. So I have linked them by using 'has_many' and 'belongs_to'.  That works but when i try to create a new owner i want to see a list of all the computers that have no owner. At the moment I can see all the computers in a multi-select box and then select some and connect them to the owner. But if that computer was already owned, the ownership just changes without me knowing.  Is there an easy was to do this. I am hoping I can just add something in my model files because thanks to rails_admin I don't really have a view file. I only have this 'application.html.erb' file:

 <!DOCTYPE html>
<html>
<head>
  <title>Testapplication</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>

Thank you in advance for an answer.

Re: filter in view with rails_admin

I'd add a scope to your computers model

i,e:

class Computer
  scope :unowned, select("name,id").joins("LEFT OUTER join owners on owners.computer_id = computers.id WHERE computers.id is NULL") 

@unowned = Computer.unowned

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: filter in view with rails_admin

Isn't that going to filter the computers every time?

I thought I 'd give some more information:
In my computers model I have a column owner_id which stores the id of the owner that computer belongs too.
Isn't it possible to check if that's null or less than zero to filter the computers?

Last edited by calidris (2012-03-26 02:55:17)

Re: filter in view with rails_admin

You posted:
i want to see a list of all the computers that have no owner

that's what I showed you how to do, 

@unowned is a list of all computers that have no owner.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.