How can I get a lightswitch to work with order() in craft.entries? I'm getting mixed results when using a lightswitch field together with order('RAND()') and I wonder if I'm doing something wrong.

For example, this code gives me the correct results:

craft.entries.section('section').lightSwitchField('1').first()


... but this selects entries from all entries in my channel instead of just the ones with the lightswitch field switched on.

craft.entries.section('section').lightSwitchField('1').order('RAND()').first()
Instead of using .first() try using .limit():

craft.entries.section('section').lightSwitchField('1').order('RAND()').limit(1)


I have just ran a test and this is working fine for me.

EDIT

I am using this code without any errors:

{% set entry = craft.entries.section('lorem').lightSwitchField('1').order('RAND()').first %}

{% if entry|length %}
    {{ entry.title }}
{% endif %}