Module: SexyScopes::ActiveRecord::DynamicMethods

Defined in:
lib/sexy_scopes/active_record/dynamic_methods.rb

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Note:

Due to the way this works, be careful not to use this syntactic sugar with existing ActiveRecord::Base methods (see last example).

Equivalent to calling #attribute with the missing method’s name if the table has a column with that name.

Delegates to superclass implementation otherwise, eventually raising NoMethodError.

Examples:

# Suppose the "users" table has an "email" column, then these are equivalent:
User.email
User.attribute(:email)
# Here is the previous example (from `attribute`) rewritten:
User.where(User.score > 1000)
# => SELECT "users".* FROM "users" WHERE ("users"."score" > 1000)
# Don't use it with existing `ActiveRecord::Base` methods, i.e. `name`:
User.name # => "User"
# In these cases you'll have to use `attribute` explicitely
User.attribute(:name)

Raises:

  • (NoMethodError)

    if the table has no corresponding column

See Also:

  • #attribute


43
44
45
46
47
48
49
50
# File 'lib/sexy_scopes/active_record/dynamic_methods.rb', line 43

def method_missing(name, *args, &block)
  if @sexy_scopes_attribute_methods_generated
    super
  else
    sexy_scopes_define_attribute_methods
    send(name, *args, &block)
  end
end