Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Find things

Alexey Gaziev edited this page · 1 revision
Clone this wiki locally

Find things

Here are unsorted material from old crappy readme related to find selectors:

Model methods and scopes

  1. Execute scope method or model method with writing it in variable with generated name

    # regular variant:
    @scope_name_table_name = TableName.scope
    
    # with Spik:
    find scope_name [from] table_name
    # or
    scope_name [from] table_name

    for example with your table posts (and model Post with :popular scope):

    # regular variant:
    @popular_posts = Post.popular
    
    # with Spik:
    find popular posts
    # or
    popular posts
    # or
    find popular from posts
  2. Execute scope method or model method with writing it in variable with custom name

    # regular variant:
    @custom_variable_name = TableName.scope
    
    # with Spik:
    find scope_name [from] table_name as custom_variable_name
    # or
    scope_name [from] table_name as custom_variable_name

    for example with your table posts (and model Post with :popular scope):

    # regular variant:
    @my_posts = Post.popular
    
    # with Spik:
    find popular posts as my_posts
    # or
    popular posts as 'my_posts'
    # or
    find popular from posts as my_posts

Select

  1. Simple select from table:

    # regular variant:
    @table_name = TableName.find(:all)
    
    # with Spik:
    find all table_name

    for example with your table posts (and model Post):

    # regular variant:
    @posts = Post.find(:all)
    
    # with Spik:
    find all posts

    After this you have initiated instance variable in your controller @posts, and you can use it in your views and later in controller

  2. Select first row from your table:

    # regular variant:
    @table_name_row = TableName.find(:first)
    
    # with Spik:
    find first table_name_row

    for example with your table posts (and model Post):

    # regular variant:
    @post = Post.find(:first)
    
    # with Spik:
    find first post

    After this you have initiated instance variable in your controller @post, and you can use it in your views and later in controller

  3. Select rows from table with some condition:

    # regular variant:
    @table_name = TableName.find(:all, :conditions => ['attribute = ?', value])
    
    # with Spike:
    find all table_name (with|which has|which have|who have|who has) (attribute_name) (id as numeric|'string in quotes')

    for example with your table posts (and model Post) and table has attribute title:

    # regular variant:
    @posts = Post.find(:all, :conditions => ['title = ?', 'asdf'])
    
    # with Spik:
    find all posts with title 'asdf'

    After this you have initiated instance variable in your controller @posts, and you can use it in your views and later in controller

  4. Select rows from table with new name of instance variable:

    # regular variant:
    @what_you_want_your_variable_name = TableName.find(:all)
    
    # with Spik:
    find all table_name as what_you_want_your_variable_name (with|which has|which have|who have| who has) (attribute_name) (id as numeric|'string in quotes')

    for example with your table posts (and model Post) and table has attribute title:

    # regular variant:
    @special_posts = Post.find(:all)
    
    # with Spik:
    find all posts as special_posts

    After this you have initiated instance variable in your controller @special_posts, and you can use it in your views and later in controller

  5. Select rows from table with attribute like something:

    # regular variant:
    @table_name = TableName.find(:all, :conditions => ['attribute LIKE "%?%"', value])
    
    # with Spik:
    find all table_name with attribute_name like 'something'

    for example with your table posts (and model Post) and table has attribute title:

    # regular variant:
    @posts = Post.find(:all, :conditions => ['title LIKE "%?%"', 'asd'])
    
    # with Spik:
    find all posts with title like 'asd'

    After this you have initiated instance variable in your controller @posts with items which have title like 'asd' (asdf|qweasdf|asd|...), and you can use it in your views and later in controller

Something went wrong with that request. Please try again.