Skip to content

Instantly share code, notes, and snippets.

@ysr23
Created October 18, 2011 12:54
Show Gist options
  • Save ysr23/1295352 to your computer and use it in GitHub Desktop.
Save ysr23/1295352 to your computer and use it in GitHub Desktop.
string to scope
so i parse my tweet:
irb(main):033:0> p qry = reply[1] + " " + reply[2]
"bbc1 now"
=> "bbc1 now"
and these are the arguments that i would like to put on the class Guide
(eventually i will need to check that these do match the named scopes i have made)
but Guide.bbc1.now does return:
irb(main):034:0> Guide.bbc1.now
=> [#<Guide id: 75493, start: "2011-10-18 12:45:00", stop: "2011-10-18 13:15:00", title: "Doctors", sub_title: "My Sunshine", description: "Rob makes a shocking discovery about an elderly man...", actors: nil, category: "Soap", episode: "12.119/231.", aspect: nil, quality: nil, created_at: "2011-10-13 08:31:02", updated_at: "2011-10-13 08:31:02", channel: "north-west.bbc1.bbc.co.uk">]
so i was just wondering about the syntax of adding that to Guide
@gma
Copy link

gma commented Oct 18, 2011

Okay, now I get you. Do you know how to write your named scopes for bbc1 and now?

I'd be tempted to make the API look like this:

Guide.for_channel('bbc1').on_at('now')

I'm not sure that Guide.bbc1 reads very well, and you don't want data from outside of your app (i.e. Twitter) to be able to choose what methods get called on your own objects. Bit of a security hole.

Look at the definition of :cheaper_than in [1] to see how to take a parameter. I'd take the class method approach rather than the lambda myself.

[1] http://asciicasts.com/episodes/215-advanced-queries-in-rails-3

@ysr23
Copy link
Author

ysr23 commented Oct 18, 2011

yes i have the scopes in my model:

scope :started, where('start < ?', Time.now+1.hour)
scope :notover, where('stop > ?', Time.now+1.hour)
scope :now, started.notover
scope :bbc1, where('channel = ?', "north-west.bbc1.bbc.co.uk")

superb graham i will take a read of that right now and see how to do it - cheers for the security tip and apols again for making this so complicated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment