1

i am pretty new to RoR so this might be a newbie question but i couldn't find an answer to it and solve the problem. I need to check every single element from a dynamic array (which is created by a HABTM connection over a join table) for a condition.

Right now i have something like this:

scope :filter, lambda { |devicefilter, testtypefilter|
  {
    testtypefilter.each { |testtype|    
        :include => :tests,
        :conditions => ['(tests.device != ? OR tests.device IS ?) AND ? NOT IN tests.testtypes', devicefilter, nil, testtype]
    }
  }
}

This shows me a syntax error. But i think even if i manage to get it working this way it would not perform the sql query correctly since ts and tts is connected by HABTM, so the actualy value in the table is NULL and i would have to create another join in that query. I have really no clue how to solve this. Any hints would be helpful.

Thanks, Niko

Edit: My Controller looks like this

if params[:d] == nil or params[:t] == nil
  @users = User.all
else
  @users = User.filter(params[:d], params[:t])
end

:d is a fixed value which is comming from a select_tag :t is an array which is comming from a bunch of dynamic checkboxes

the values are all fine and being passed correctly to the Model. So the view or controller shouldn't be the problem.

Edit2: Since it looks a bit unclear, here is the current situation: There is a form which consists of a Select-Box and a bunch of Checkboxes. The Select-Box is getting its values from the table "devices" and after the form is submitted it passes the selected value to the controller as the param :d. The Checkboxes are a list from all entries of the table "testtypes" and after the form is submitted it passes an array of all checked testtype_ids from the table to the controller as the param :t. (works correctly)

Now there is another table "tests" which has a HABTM connection to all entries in devices and testtypes, so i can gather special connections from 1 device and and multiple testtypes together in a single entry.

Now in the table "users" there is a column "tests" which refers to the table "tests" in a HABTM connection so 1 user can have (participated in) multiple tests (which can have multiple testtypes).

So after the form is submitted the values :d and :t should be used for a sql query to filter all users by tests (while tests are dependant by devices and testtypes). So all users should by checked in the column tests by the values of the referring tests in devices (:d) and testtypes (:t[]).

But since testtypes (:t[]) is being passed as an array i have to check somehow every single element of that array with a sql request. Or is there a way to check for the whole array?

Example:

There are 3 Devices: (dynamic table)
A01
A02
A03
There are 3 Testtypes: (dynamic table)
TT01
TT02
TT03
There are 4 Tests: (dynamic table with HABTM devices and testtypes)
T1 = A01 - TT01/TT03
T2 = A01 - TT03
T3 = A02 - TT02/TT03
T4 = A03 - TT01/TT02/TT03
There are 5 Users: (dynamic table with HABTM tests)
U1 = ... T1 ...
U2 = ... T2/T4 ...
U3 = ... T3/T4 ...
U4 = ... T1/T2/T3 ...
U5 = ... T3/T4 ...

Now the Form will look like:
Device: Select-Box={A01/A02/A03}
Testtypes: Checkboxes={TT01/TT02/TT03}

If i select now A01, check TT01+TT02 and submit the query should return every single User who has not participated in the following Tests:
A01 - TT01
A01 - TT02

So at last i get a list of users that i could use for the test A01 - TT01+TT03, since there are no conflicts.
So the query would return every user who has not participated in T1, since that is the only conflict.
So the userlist would look like:
U2
U3
U5

Can someone help me with this? Noone got an idea? :'(

7
  • Tell us what you have, and what you want exactly, because you're really going the wrong way with this I think ;) Commented Dec 21, 2011 at 12:45
  • I have a form which consists of a select-box and a few checkboxes (which can change). This form should be used to filter a table by the column "ts" in the table of all Pts. The column ts consists of 2 values, since it is another table (ts) with HABTM (1. d, 2. t). And those ts have another HABTM connection to d and to t. d is a fixed value comming (selected by select_tag) from the table d and t is an array comming from the table t (selected by checkboxes). What i want: Simply the filter working and passing a correct sql request so i get the filtered table. :) Commented Dec 21, 2011 at 13:05
  • You get the syntax error because you shouldn't use { after lambda { |df, ttf|. Commented Dec 21, 2011 at 13:25
  • Okay the syntax error is fixed, thanks alot :-) But as forseen now the sql request is not working properly. I get an empty list as soon as i check a single checkbox (which should not be) Commented Dec 21, 2011 at 13:48
  • I don't know if you are using short variable names for stackoverflow, or if it's like that in your code, but it makes it a bit harder for us to understand your code. Commented Dec 21, 2011 at 14:01

1 Answer 1

3

Sounds like you want something like

scope :filter, lambda {|df| includes(:ts).where("foo_id in (?)", df)

which will allow you to do

SomeModel.filter([23,24])

And that that array would be passed into the lambda for you to use in the conditions.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer, that is exactly how it should be working. I added the controller part to the question. The values are being passed correctly, but the problem is, that the 23 in your example can be an array and i do not know how to wirte the correct sql query for that.
That shouldn't make much difference. I've updated the example.
But i am checking if an array is included in another array and this does not work that way. Maybe you understood me, i updated the Question to clear things up. Thanks for the help tho :)
Your conditions can refer to the join table as well as the table from the association. Other than that I don't understand what you're getting at
I have to check that array in a second join (users=>tests=>testtypes) The filter should check if each of those ids from the array is not in the testtypes table from each of the tests of each user. I have no idea how i should check that with a simple sql query. If every single "checked" (by the checkboxes) testtype-id is not included in any of the tests the user participated in, then this user is one of those who shall be returned by the query.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.