method
method_missing
v3.0.9 -
Show latest stable
- Class:
ActiveRecord::Base
method_missing(method_id, *arguments, &block)private
Enables dynamic finders like User.find_by_user_name(user_name) and <tt>User.scoped_by_user_name(user_name). Refer to Dynamic attribute-based finders section at the top of this file for more detailed information.
It’s even possible to use all the additional parameters to find. For example, the full interface for find_all_by_amount is actually find_all_by_amount(amount, options).
Each dynamic finder using scoped_by_* is also defined in the class after it is first invoked, so that future attempts to use it do not run through method_missing.
# File activerecord/lib/active_record/base.rb, line 986
def method_missing(method_id, *arguments, &block)
if match = DynamicFinderMatch.match(method_id)
attribute_names = match.attribute_names
super unless all_attributes_exists?(attribute_names)
if match.finder?
options = arguments.extract_options!
relation = options.any? ? construct_finder_arel(options, current_scoped_methods) : scoped
relation.send :find_by_attributes, match, attribute_names, *arguments
elsif match.instantiator?
scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block
end
elsif match = DynamicScopeMatch.match(method_id)
attribute_names = match.attribute_names
super unless all_attributes_exists?(attribute_names)
if match.scope?
self.class_eval def self.#{method_id}(*args) # def self.scoped_by_user_name_and_password(*args) options = args.extract_options! # options = args.extract_options! attributes = construct_attributes_from_arguments( # attributes = construct_attributes_from_arguments( [:#{attribute_names.join(',:')}], args # [:user_name, :password], args ) # ) # scoped(:conditions => attributes) # scoped(:conditions => attributes) end # end, __FILE__, __LINE__ + 1
send(method_id, *arguments)
end
else
super
end
end Related methods
- Instance methods
- ==
- []
- []=
- attribute_for_inspect
- attribute_names
- attribute_present?
- attributes
- attributes=
- cache_key
- column_for_attribute
- connection
- dup
- eql?
- freeze
- frozen?
- has_attribute?
- hash
- init_with
- initialize_copy
- inspect
- quoted_id
- readonly!
- readonly?
- to_param
- Class methods
- ===
- abstract_class?
- arel_engine
- arel_table
- attr_readonly
- attribute_method?
- base_class
- before_remove_const
- colorize_logging
- colorize_logging=
- column_methods_hash
- column_names
- columns
- columns_hash
- configurations
- connected?
- connection
- connection_handler
- connection_pool
- content_columns
- count_by_sql
- create
- default_timezone
- descends_from_active_record?
- establish_connection
- find_by_sql
- finder_needs_type_condition?
- full_table_name_prefix
- i18n_scope
- inheritance_column
- inheritance_column=
- inspect
- logger
- lookup_ancestors
- mysql_connection
- new
- pluralize_table_names
- postgresql_connection
- primary_key_prefix_type
- quote_value
- quoted_table_name
- readonly_attributes
- remove_connection
- reset_column_information
- reset_column_information_and_inheritable_attributes_for_all_subclasses
- reset_sequence_name
- reset_subclasses
- reset_table_name
- respond_to?
- retrieve_connection
- sanitize
- schema_format
- scoped_methods
- sequence_name
- sequence_name=
- serialize
- serialized_attributes
- set_inheritance_column
- set_sequence_name
- set_table_name
- sqlite3_connection
- sti_name
- subclasses
- table_exists?
- table_name
- table_name=
- table_name_prefix
- table_name_suffix
- timestamped_migrations
- unscoped
- Protected methods
-
aggregate_mapping -
class_of_active_record_descendant -
compute_type -
current_scoped_methods -
default_scope -
encode_quoted_value -
expand_hash_conditions_for_aggregates -
expand_range_bind_variables -
quote_bound_value -
raise_if_bind_arity_mismatch -
replace_bind_variables -
replace_named_bind_variables -
reset_scoped_methods -
sanitize_conditions -
sanitize_sql -
sanitize_sql_array -
sanitize_sql_for_assignment -
sanitize_sql_for_conditions -
sanitize_sql_hash -
sanitize_sql_hash_for_assignment -
sanitize_sql_hash_for_conditions -
with_exclusive_scope -
with_scope -
clone_attribute_value -
clone_attributes - Private methods
-
all_attributes_exists? -
attributes_protected_by_default -
compute_table_name -
construct_attributes_from_arguments -
construct_finder_arel -
expand_attribute_names_for_aggregates -
find_sti_class -
instantiate -
method_missing -
relation -
type_condition -
undecorated_table_name -
arel_attributes_values -
assign_multiparameter_attributes -
comma_pair_list -
convert_number_column_value -
ensure_proper_type -
execute_callstack_for_multiparameter_attributes -
extract_callstack_for_multiparameter_attributes -
find_parameter_position -
instantiate_time_object -
interpolate_and_sanitize_sql -
interpolate_sanitized_sql -
object_from_yaml -
populate_with_current_scope_attributes -
quote_columns -
quote_value -
quoted_comma_pair_list -
type_cast_attribute_value