0

I need filter 'apps' in my rails project based on whether they support iphone. I am trying to create a named scope for this purpose but I am getting the following error.

app/models/app.rb:21: syntax error, unexpected '}', expecting =>
app/models/app.rb:21: syntax error, unexpected '}', expecting =>
app/controllers/apps_controller.rb:5:in `index'

app.rb

named_scope :with_iphone_support,
              joins: :version,
              conditions: {:versions.any{ |v| v.iphone_support == true}}
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188

2 Answers2

1

conditions expects a hash, not a block.

Try this:

named_scope :with_iphone_support,
             conditions: 'exists(select * from versions where iphone_support = 1 and app_id = apps.id)'
eritiro
  • 306
  • 1
  • 6
1

Generally it should look something like this for Rails 2:

named_scope :with_iphone_support,
  joins: :version,
  conditions: { versions: { iphone_support: true } }

It's not possible to pass in a block to something like that.

Rails 3.x and newer use the scope + where method for defining scopes.

tadman
  • 208,517
  • 23
  • 234
  • 262