I have 2 models (player and team linked through the model lnkteamplayer)
Team has_many players through lnkteamplayer
Player has_many teams through lnkteamplayer
I need to retrieve all players not belonging to a specific team.
<% @players = Player.find(:all, :conditions => ["id != ?",@team.lnkteamplayers.player_id ]) %>
I am getting an error with above line of code. My question is how do i pass an array of values in the above condition.
Thanks for any suggestion provided.
You’ve got a couple of problems there:
1) the first part of conditions, “id != ?”, is a fragment of sql, and in sql you do “not equals” as
<>not!=. Eg"id <> ?"2) To use an array, the sql syntax is
id in (1,2,3)orid not in (1,2,3). In your conditions you can do this like:conditions => ["id not in (?)", array_of_ids]So, you could get players not on a team like this: