How can I find records whose :name do NOT equal an array provided by another query?

Currently my users can add locations to their profiles via a form which includes this statement: (I'm using RoR3, HAML, sqlite3 for dev, and mysql for prod)

= select_tag "id", options_from_collection_for_select(Location.all, 'id', 'name')

However, this allows the user to add the same location multiple times. I would like to list only the locations which the user has NOT already posted. So I would like to do something like:

Location.find(:all, :conditions => ["name != ?", user.locations])

This of course does not work whereas this does.

Location.find(:all, :conditions => ["name != ?", "New York"])

That's because user.locations returns an array. I haven't the slightest idea how to proceed at this point. Other than learning SQL I suppose. Is there a method for this that I'm not finding?

Answers


Something like:

Location.find(:all, :conditions => ["name not in (?)", user.locations]) 

should do it (although admittedly less efficient than doing an outer join and filtering null user_ids ) depending on what your array of "user.locations" actually are.

As a side note, learning SQL will make you a much more capable (and marketable) web-developer ... food for thought.


Need Your Help

PHP form action script now working

php javascript jquery html css

Script1 is based on CSS and html design(with some texture effect) I tried to setup php form action script to a html/css form however it keeps refreshing to the home page.. according to script if

Where do I place str_replace(' ', '-', to remove spaces from url and load proper sql data

php mysql sql url

I am able to add dashes to the url with the help from previous post but, I can not get the proper page to load once i complete this task.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.