workspace does not return the correct value for me

Viewed 55

i have problem with some code.

If i write Recenzes select: [:a | a komponenta nazev = 'Hitachi P21'] i got some right records. But if i use something like this:

| brzdy |
brzdy := (((
   (Sekces select: [:b | b nazev = 'Brzdy']) collect: [:b | b komponenty]) flatten)
       select: [:c | c vyrobce nazev = 'Hitachi']) collect: [:d | d nazev].

i can get 'Hitachi P21' with ^ command. But if i use variable 'brzdy' here: Recenzes select: [:a | a komponenta nazev = brzdy] i won't get anything.

In a nutshell. I want to show 'Recenzes' for 'Komponenty' which are in 'Sekces' with value 'Brzdy' and they are saved in column 'Komponenty' (Set) for 'Recenzes' and 'Sekces'.

Does anyone know why?

1 Answers

Since brzdy is the result of a #collect: message, it is a collection of strings, not a single string. Therefore no element a would satisfy the condition a komponenta nazev = brzdy, because you would be comparing objects of different classes. Try something on the lines of

Recenzes select: [:a | brzdy includes: a komponenta nazev]

As a side note, remember that you may eliminate some parentheses by using select:thenCollect: other than (select: blah) collect: bluh. For instance

brzdy := (Sekces select: [:b | b nazev = 'Brzdy'] thenCollect: [:b | b komponenty]) flatten
        select: [:c | c vyrobce nazev = 'Hitachi']
        thenCollect: [:d | d nazev]

(I'm not familiar with the #flatten message, so I can't tell whether it is necessary or superfluous).

Related