开发者

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 findi开发者_C百科ng?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜