<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Class: ActiveRecord::Base</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Class</strong></td> <td class="class-name-in-header">ActiveRecord::Base</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../files/lib/active_record/base_rb.html"> lib/active_record/base.rb </a> <br /> <a href="../../files/lib/active_record/connection_adapters/mysql_adapter_rb.html"> lib/active_record/connection_adapters/mysql_adapter.rb </a> <br /> <a href="../../files/lib/active_record/connection_adapters/postgresql_adapter_rb.html"> lib/active_record/connection_adapters/postgresql_adapter.rb </a> <br /> <a href="../../files/lib/active_record/connection_adapters/abstract/connection_specification_rb.html"> lib/active_record/connection_adapters/abstract/connection_specification.rb </a> <br /> <a href="../../files/lib/active_record/connection_adapters/sqlite3_adapter_rb.html"> lib/active_record/connection_adapters/sqlite3_adapter.rb </a> <br /> <a href="../../files/lib/active_record/connection_adapters/sqlite_adapter_rb.html"> lib/active_record/connection_adapters/sqlite_adapter.rb </a> <br /> </td> </tr> <tr class="top-aligned-row"> <td><strong>Parent:</strong></td> <td> Object </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> Active Record objects don‘t specify their <a href="Base.html#M000543">attributes</a> directly, but rather infer them from the table definition with which they‘re linked. Adding, removing, and changing <a href="Base.html#M000543">attributes</a> and their type is done directly in the database. Any change is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain database table will happen automatically in most common cases, but can be overwritten for the uncommon ones. </p> <p> See the mapping rules in <a href="Base.html#M000481">table_name</a> and the full example in <a href="../../files/README.html">files/README.html</a> for more insight. </p> <h2>Creation</h2> <p> Active Records accept constructor parameters either in a <a href="Base.html#M000552">hash</a> or as a block. The <a href="Base.html#M000552">hash</a> method is especially useful when you‘re receiving the data from somewhere else, like an HTTP request. It works like this: </p> <pre> user = User.new(:name => "David", :occupation => "Code Artist") user.name # => "David" </pre> <p> You can also use block initialization: </p> <pre> user = User.new do |u| u.name = "David" u.occupation = "Code Artist" end </pre> <p> And of course you can just <a href="Base.html#M000464">create</a> a bare object and specify the <a href="Base.html#M000543">attributes</a> after the fact: </p> <pre> user = User.new user.name = "David" user.occupation = "Code Artist" </pre> <h2>Conditions</h2> <p> Conditions can either be specified as a string, array, or <a href="Base.html#M000552">hash</a> representing the WHERE-part of an SQL statement. The array form is to be used when the condition input is tainted and requires sanitization. The string form can be used for statements that don‘t involve tainted data. The <a href="Base.html#M000552">hash</a> form works much like the array form, except only equality and range is possible. Examples: </p> <pre> class User < ActiveRecord::Base def self.authenticate_unsafely(user_name, password) find(:first, :conditions => "user_name = '#{user_name}' AND password = '#{password}'") end def self.authenticate_safely(user_name, password) find(:first, :conditions => [ "user_name = ? AND password = ?", user_name, password ]) end def self.authenticate_safely_simply(user_name, password) find(:first, :conditions => { :user_name => user_name, :password => password }) end end </pre> <p> The <tt>authenticate_unsafely</tt> method inserts the parameters directly into the query and is thus susceptible to SQL-injection attacks if the <tt>user_name</tt> and <tt>password</tt> parameters come directly from an HTTP request. The <tt>authenticate_safely</tt> and <tt>authenticate_safely_simply</tt> both will sanitize the <tt>user_name</tt> and <tt>password</tt> before inserting them in the query, which will ensure that an attacker can‘t escape the query and fake the login (or worse). </p> <p> When using multiple parameters in the conditions, it can easily become hard to read exactly what the fourth or fifth question mark is supposed to represent. In those cases, you can resort to named bind variables instead. That‘s done by replacing the question marks with symbols and supplying a <a href="Base.html#M000552">hash</a> with values for the matching symbol keys: </p> <pre> Company.find(:first, :conditions => [ "id = :id AND name = :name AND division = :division AND created_at > :accounting_date", { :id => 3, :name => "37signals", :division => "First", :accounting_date => '2005-01-01' } ]) </pre> <p> Similarly, a simple <a href="Base.html#M000552">hash</a> without a statement will generate conditions based on equality with the SQL AND operator. For instance: </p> <pre> Student.find(:all, :conditions => { :first_name => "Harvey", :status => 1 }) Student.find(:all, :conditions => params[:student]) </pre> <p> A range may be used in the <a href="Base.html#M000552">hash</a> to use the SQL BETWEEN operator: </p> <pre> Student.find(:all, :conditions => { :grade => 9..12 }) </pre> <p> An array may be used in the <a href="Base.html#M000552">hash</a> to use the SQL IN operator: </p> <pre> Student.find(:all, :conditions => { :grade => [9,11,12] }) </pre> <h2>Overwriting default accessors</h2> <p> All column values are automatically available through basic accessors on the Active Record object, but sometimes you want to specialize this behavior. This can be done by overwriting the default accessors (using the same name as the attribute) and calling <tt>read_attribute(attr_name)</tt> and <tt>write_attribute(attr_name, value)</tt> to actually change things. Example: </p> <pre> class Song < ActiveRecord::Base # Uses an integer of seconds to hold the length of the song def length=(minutes) write_attribute(:length, minutes.to_i * 60) end def length read_attribute(:length) / 60 end end </pre> <p> You can alternatively use <tt>self[:attribute]=(value)</tt> and <tt>self[:attribute]</tt> instead of <tt>write_attribute(:attribute, value)</tt> and <tt>read_attribute(:attribute)</tt> as a shorter form. </p> <h2>Attribute query methods</h2> <p> In addition to the basic accessors, query methods are also automatically available on the Active Record object. Query methods allow you to test whether an attribute value is present. </p> <p> For example, an Active Record User with the <tt>name</tt> attribute has a <tt>name?</tt> method that you can call to determine whether the user has a name: </p> <pre> user = User.new(:name => "David") user.name? # => true anonymous = User.new(:name => "") anonymous.name? # => false </pre> <h2>Accessing <a href="Base.html#M000543">attributes</a> before they have been typecasted</h2> <p> Sometimes you want to be able to read the raw attribute data without having the column-determined typecast run its course <a href="Base.html#M000459">first</a>. That can be done by using the <tt><attribute>_before_type_cast</tt> accessors that <a href="Base.html#M000461">all</a> <a href="Base.html#M000543">attributes</a> have. For example, if your Account model has a <tt>balance</tt> attribute, you can call <tt>account.balance_before_type_cast</tt> or <tt>account.id_before_type_cast</tt>. </p> <p> This is especially useful in validation situations where the user might supply a string for an integer field and you want to display the original string back in an error message. Accessing the attribute normally would typecast the string to 0, which isn‘t what you want. </p> <h2>Dynamic attribute-based finders</h2> <p> Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects by simple queries without turning to SQL. They work by appending the name of an attribute to <tt>find_by_</tt>, <tt>find_last_by_</tt>, or <tt>find_all_by_</tt>, so you get finders like <tt>Person.find_by_user_name</tt>, <tt>Person.find_all_by_last_name</tt>, and <tt>Payment.find_by_transaction_id</tt>. So instead of writing <tt>Person.find(:<a href="Base.html#M000459">first</a>, :conditions => ["user_name = ?", user_name])</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>. And instead of writing <tt>Person.find(:<a href="Base.html#M000461">all</a>, :conditions => ["last_name = ?", last_name])</tt>, you just do <tt>Person.find_all_by_last_name(last_name)</tt>. </p> <p> It‘s also possible to use multiple <a href="Base.html#M000543">attributes</a> in the same <a href="Base.html#M000458">find</a> by separating them with "<em>and</em>", so you get finders like <tt>Person.find_by_user_name_and_password</tt> or even <tt>Payment.find_by_purchaser_and_state_and_country</tt>. So instead of writing <tt>Person.find(:<a href="Base.html#M000459">first</a>, :conditions => ["user_name = ? AND password = ?", user_name, password])</tt>, you just do <tt>Person.find_by_user_name_and_password(user_name, password)</tt>. </p> <p> It‘s even possible to use <a href="Base.html#M000461">all</a> the additional parameters to <a href="Base.html#M000458">find</a>. For example, the full interface for <tt>Payment.find_all_by_amount</tt> is actually <tt>Payment.find_all_by_amount(amount, options)</tt>. And the full interface to <tt>Person.find_by_user_name</tt> is actually <tt>Person.find_by_user_name(user_name, options)</tt>. So you could call <tt>Payment.find_all_by_amount(50, :order => "created_on")</tt>. Also you may call <tt>Payment.find_last_by_amount(amount, options)</tt> returning the <a href="Base.html#M000460">last</a> record matching that amount and options. </p> <p> The same dynamic finder style can be used to <a href="Base.html#M000464">create</a> the object if it doesn‘t already exist. This dynamic finder is called with <tt>find_or_create_by_</tt> and will return the object if it already exists and otherwise creates it, then returns it. Protected <a href="Base.html#M000543">attributes</a> won‘t be set unless they are given in a block. For example: </p> <pre> # No 'Summer' tag exists Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer") # Now the 'Summer' tag does exist Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer") # Now 'Bob' exist and is an 'admin' User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true } </pre> <p> Use the <tt>find_or_initialize_by_</tt> finder if you want to return a <a href="Base.html#M000518">new</a> record without saving it <a href="Base.html#M000459">first</a>. Protected <a href="Base.html#M000543">attributes</a> won‘t be set unless they are given in a block. For example: </p> <pre> # No 'Winter' tag exists winter = Tag.find_or_initialize_by_name("Winter") winter.new_record? # true </pre> <p> To <a href="Base.html#M000458">find</a> by a subset of the <a href="Base.html#M000543">attributes</a> to be used for instantiating a <a href="Base.html#M000518">new</a> object, pass a <a href="Base.html#M000552">hash</a> instead of a list of parameters. For example: </p> <pre> Tag.find_or_create_by_name(:name => "rails", :creator => current_user) </pre> <p> That will either <a href="Base.html#M000458">find</a> an existing tag named "rails", or <a href="Base.html#M000464">create</a> a <a href="Base.html#M000518">new</a> one while setting the user that created it. </p> <h2>Saving arrays, hashes, and other non-mappable objects in text <a href="Base.html#M000489">columns</a></h2> <p> Active Record can <a href="Base.html#M000479">serialize</a> any object in text <a href="Base.html#M000489">columns</a> using YAML. To do so, you must specify this with a call to the class method <tt><a href="Base.html#M000479">serialize</a></tt>. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work. Example: </p> <pre> class User < ActiveRecord::Base serialize :preferences end user = User.create(:preferences => { "background" => "black", "display" => large }) User.find(user.id).preferences # => { "background" => "black", "display" => large } </pre> <p> You can also specify a class option as the second parameter that‘ll raise an exception if a serialized object is retrieved as a descendant of a class not in the hierarchy. Example: </p> <pre> class User < ActiveRecord::Base serialize :preferences, Hash end user = User.create(:preferences => %w( one two three )) User.find(user.id).preferences # raises SerializationTypeMismatch </pre> <h2>Single table inheritance</h2> <p> Active Record allows inheritance by storing the name of the class in a column that by default is named "type" (can be changed by overwriting <tt><a href="Base.html#M000483">Base.inheritance_column</a></tt>). This means that an inheritance looking like this: </p> <pre> class Company < ActiveRecord::Base; end class Firm < Company; end class Client < Company; end class PriorityClient < Client; end </pre> <p> When you do <tt>Firm.create(:name => "37signals")</tt>, this record will be saved in the companies table with type = "Firm". You can then fetch this row again using <tt>Company.find(:<a href="Base.html#M000459">first</a>, "name = ‘37signals’")</tt> and it will return a Firm object. </p> <p> If you don‘t have a type column defined in your table, single-table inheritance won‘t be triggered. In that case, it‘ll work just like normal subclasses with no special magic for differentiating between them or reloading the right type with <a href="Base.html#M000458">find</a>. </p> <p> Note, <a href="Base.html#M000461">all</a> the <a href="Base.html#M000543">attributes</a> for <a href="Base.html#M000461">all</a> the cases are kept in the same table. Read more: <a href="http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html">www.martinfowler.com/eaaCatalog/singleTableInheritance.html</a> </p> <h2>Connection to multiple databases in different models</h2> <p> Connections are usually created through <a href="Base.html#M000559">ActiveRecord::Base.establish_connection</a> and retrieved by <a href="Base.html#M000558">ActiveRecord::Base.connection</a>. All classes inheriting from <a href="Base.html">ActiveRecord::Base</a> will use this <a href="Base.html#M000558">connection</a>. But you can also set a class-specific <a href="Base.html#M000558">connection</a>. For example, if Course is an <a href="Base.html">ActiveRecord::Base</a>, but resides in a different database, you can just say <tt>Course.establish_connection</tt> and Course and <a href="Base.html#M000461">all</a> of its subclasses will use this <a href="Base.html#M000558">connection</a> instead. </p> <p> This feature is implemented by keeping a <a href="Base.html#M000558">connection</a> pool in <a href="Base.html">ActiveRecord::Base</a> that is a Hash indexed by the class. If a <a href="Base.html#M000558">connection</a> is requested, the <a href="Base.html#M000566">retrieve_connection</a> method will go up the class-hierarchy until a <a href="Base.html#M000558">connection</a> is found in the <a href="Base.html#M000558">connection</a> pool. </p> <h2>Exceptions</h2> <ul> <li><a href="ActiveRecordError.html">ActiveRecordError</a> - Generic error class and superclass of <a href="Base.html#M000461">all</a> other errors raised by Active Record. </li> <li><a href="AdapterNotSpecified.html">AdapterNotSpecified</a> - The configuration <a href="Base.html#M000552">hash</a> used in <tt><a href="Base.html#M000559">establish_connection</a></tt> didn‘t include an <tt>:adapter</tt> key. </li> <li><a href="AdapterNotFound.html">AdapterNotFound</a> - The <tt>:adapter</tt> key used in <tt><a href="Base.html#M000559">establish_connection</a></tt> specified a non-existent adapter (or a bad spelling of an existing one). </li> <li><a href="AssociationTypeMismatch.html">AssociationTypeMismatch</a> - The object assigned to the association wasn‘t of the type specified in the association definition. </li> <li><a href="SerializationTypeMismatch.html">SerializationTypeMismatch</a> - The serialized object wasn‘t of the class specified as the second parameter. </li> <li><a href="ConnectionNotEstablished.html">ConnectionNotEstablished</a>+ - No <a href="Base.html#M000558">connection</a> has been established. Use <tt><a href="Base.html#M000559">establish_connection</a></tt> before querying. </li> <li><a href="RecordNotFound.html">RecordNotFound</a> - No record responded to the <tt><a href="Base.html#M000458">find</a></tt> method. Either the row with the given ID doesn‘t exist or the row didn‘t meet the additional restrictions. Some <tt><a href="Base.html#M000458">find</a></tt> calls do not raise this exception to signal nothing was found, please check its documentation for further details. </li> <li><a href="StatementInvalid.html">StatementInvalid</a> - The database server rejected the SQL statement. The precise error is added in the message. </li> <li><a href="MultiparameterAssignmentErrors.html">MultiparameterAssignmentErrors</a> - Collection of errors that occurred during a mass assignment using the <tt><a href="Base.html#M000542">attributes=</a></tt> method. The <tt>errors</tt> property of this exception contains an array of <a href="AttributeAssignmentError.html">AttributeAssignmentError</a> objects that should be inspected to determine which <a href="Base.html#M000543">attributes</a> triggered the errors. </li> <li><a href="AttributeAssignmentError.html">AttributeAssignmentError</a> - An error occurred while doing a mass assignment through the <tt><a href="Base.html#M000542">attributes=</a></tt> method. You can <a href="Base.html#M000497">inspect</a> the <tt>attribute</tt> property of the exception object to determine which attribute triggered the error. </li> </ul> <p> <b>Note</b>: The <a href="Base.html#M000543">attributes</a> listed are class-level <a href="Base.html#M000543">attributes</a> (accessible from both the class and instance level). So it‘s possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by <a href="Base.html#M000461">all</a> instances in the current object space. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000550">==</a> <a href="#M000500">===</a> <a href="#M000540">[]</a> <a href="#M000541">[]=</a> <a href="#M000502">abstract_class?</a> <a href="#M000513">aggregate_mapping</a> <a href="#M000461">all</a> <a href="#M000560">allow_concurrency</a> <a href="#M000561">allow_concurrency=</a> <a href="#M000476">attr_accessible</a> <a href="#M000475">attr_protected</a> <a href="#M000477">attr_readonly</a> <a href="#M000545">attribute_for_inspect</a> <a href="#M000548">attribute_names</a> <a href="#M000546">attribute_present?</a> <a href="#M000543">attributes</a> <a href="#M000542">attributes=</a> <a href="#M000544">attributes_before_type_cast</a> <a href="#M000501">base_class</a> <a href="#M000529">becomes</a> <a href="#M000498">benchmark</a> <a href="#M000521">cache_key</a> <a href="#M000510">class_of_active_record_descendant</a> <a href="#M000528">clone</a> <a href="#M000549">column_for_attribute</a> <a href="#M000491">column_names</a> <a href="#M000489">columns</a> <a href="#M000490">columns_hash</a> <a href="#M000509">compute_type</a> <a href="#M000567">connected?</a> <a href="#M000558">connection</a> <a href="#M000564">connection</a> <a href="#M000565">connection_pool</a> <a href="#M000492">content_columns</a> <a href="#M000471">count_by_sql</a> <a href="#M000464">create</a> <a href="#M000535">decrement</a> <a href="#M000536">decrement!</a> <a href="#M000474">decrement_counter</a> <a href="#M000508">default_scope</a> <a href="#M000466">delete</a> <a href="#M000526">delete</a> <a href="#M000470">delete_all</a> <a href="#M000496">descends_from_active_record?</a> <a href="#M000527">destroy</a> <a href="#M000467">destroy</a> <a href="#M000469">destroy_all</a> <a href="#M000551">eql?</a> <a href="#M000559">establish_connection</a> <a href="#M000463">exists?</a> <a href="#M000514">expand_hash_conditions_for_aggregates</a> <a href="#M000458">find</a> <a href="#M000462">find_by_sql</a> <a href="#M000459">first</a> <a href="#M000553">freeze</a> <a href="#M000554">frozen?</a> <a href="#M000547">has_attribute?</a> <a href="#M000552">hash</a> <a href="#M000494">human_attribute_name</a> <a href="#M000495">human_name</a> <a href="#M000519">id</a> <a href="#M000522">id=</a> <a href="#M000533">increment</a> <a href="#M000534">increment!</a> <a href="#M000473">increment_counter</a> <a href="#M000483">inheritance_column</a> <a href="#M000557">inspect</a> <a href="#M000497">inspect</a> <a href="#M000460">last</a> <a href="#M000505">merge_conditions</a> <a href="#M000518">new</a> <a href="#M000523">new_record?</a> <a href="#M000482">primary_key</a> <a href="#M000556">readonly!</a> <a href="#M000555">readonly?</a> <a href="#M000478">readonly_attributes</a> <a href="#M000539">reload</a> <a href="#M000568">remove_connection</a> <a href="#M000493">reset_column_information</a> <a href="#M000503">respond_to?</a> <a href="#M000566">retrieve_connection</a> <a href="#M000517">sanitize_sql_array</a> <a href="#M000512">sanitize_sql_for_assignment</a> <a href="#M000511">sanitize_sql_for_conditions</a> <a href="#M000516">sanitize_sql_hash_for_assignment</a> <a href="#M000515">sanitize_sql_hash_for_conditions</a> <a href="#M000524">save</a> <a href="#M000525">save!</a> <a href="#M000479">serialize</a> <a href="#M000480">serialized_attributes</a> <a href="#M000486">set_inheritance_column</a> <a href="#M000485">set_primary_key</a> <a href="#M000487">set_sequence_name</a> <a href="#M000484">set_table_name</a> <a href="#M000499">silence</a> <a href="#M000504">sti_name</a> <a href="#M000488">table_exists?</a> <a href="#M000481">table_name</a> <a href="#M000520">to_param</a> <a href="#M000537">toggle</a> <a href="#M000538">toggle!</a> <a href="#M000465">update</a> <a href="#M000468">update_all</a> <a href="#M000530">update_attribute</a> <a href="#M000531">update_attributes</a> <a href="#M000532">update_attributes!</a> <a href="#M000472">update_counters</a> <a href="#M000562">verification_timeout</a> <a href="#M000563">verification_timeout=</a> <a href="#M000507">with_exclusive_scope</a> <a href="#M000506">with_scope</a> </div> </div> </div> <!-- if includes --> <div id="section"> <div id="constants-list"> <h3 class="section-bar">Constants</h3> <div class="name-list"> <table summary="Constants"> <tr class="top-aligned-row context-row"> <td class="context-item-name">VALID_FIND_OPTIONS</td> <td>=</td> <td class="context-item-value">[ :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from, :lock ]</td> </tr> </table> </div> </div> <div id="aliases-list"> <h3 class="section-bar">External Aliases</h3> <div class="name-list"> <table summary="aliases"> <tr class="top-aligned-row context-row"> <td class="context-item-name">set_table_name</td> <td>-></td> <td class="context-item-value">table_name=</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">set_primary_key</td> <td>-></td> <td class="context-item-value">primary_key=</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">set_inheritance_column</td> <td>-></td> <td class="context-item-value">inheritance_column=</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">set_sequence_name</td> <td>-></td> <td class="context-item-value">sequence_name=</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">sanitize_sql_for_conditions</td> <td>-></td> <td class="context-item-value">sanitize_sql</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">sanitize_sql_hash_for_conditions</td> <td>-></td> <td class="context-item-value">sanitize_sql_hash</td> </tr> <tr class="top-aligned-row context-row"> <td class="context-item-name">sanitize_sql</td> <td>-></td> <td class="context-item-value">sanitize_conditions</td> </tr> </table> </div> </div> <div id="attribute-list"> <h3 class="section-bar">Attributes</h3> <div class="name-list"> <table> <tr class="top-aligned-row context-row"> <td class="context-item-name">abstract_class</td> <td class="context-item-value"> [RW] </td> <td class="context-item-desc"> Set this to true if this is an abstract class (see <tt><a href="Base.html#M000502">abstract_class?</a></tt>). </td> </tr> </table> </div> </div> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Class methods</h3> <div id="method-M000500" class="method-detail"> <a name="M000500"></a> <div class="method-heading"> <a href="Base.src/M000500.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000500.html');return false;"> <span class="method-name">===</span><span class="method-args">(object)</span> </a> </div> <div class="method-description"> <p> Overwrite the default class equality method to provide support for association proxies. </p> </div> </div> <div id="method-M000502" class="method-detail"> <a name="M000502"></a> <div class="method-heading"> <a href="Base.src/M000502.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000502.html');return false;"> <span class="method-name">abstract_class?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns whether this class is a base AR class. If A is a base class and B descends from A, then B.base_class will return B. </p> </div> </div> <div id="method-M000461" class="method-detail"> <a name="M000461"></a> <div class="method-heading"> <a href="Base.src/M000461.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000461.html');return false;"> <span class="method-name">all</span><span class="method-args">(*args)</span> </a> </div> <div class="method-description"> <p> This is an alias for <a href="Base.html#M000458">find</a>(:<a href="Base.html#M000461">all</a>). You can pass in <a href="Base.html#M000461">all</a> the same arguments to this method as you can to <a href="Base.html#M000458">find</a>(:<a href="Base.html#M000461">all</a>) </p> </div> </div> <div id="method-M000560" class="method-detail"> <a name="M000560"></a> <div class="method-heading"> <a href="Base.src/M000560.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000560.html');return false;"> <span class="method-name">allow_concurrency</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Deprecated and no longer has any effect. </p> </div> </div> <div id="method-M000561" class="method-detail"> <a name="M000561"></a> <div class="method-heading"> <a href="Base.src/M000561.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000561.html');return false;"> <span class="method-name">allow_concurrency=</span><span class="method-args">(flag)</span> </a> </div> <div class="method-description"> <p> Deprecated and no longer has any effect. </p> </div> </div> <div id="method-M000476" class="method-detail"> <a name="M000476"></a> <div class="method-heading"> <a href="Base.src/M000476.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000476.html');return false;"> <span class="method-name">attr_accessible</span><span class="method-args">(*attributes)</span> </a> </div> <div class="method-description"> <p> Specifies a white list of model <a href="Base.html#M000543">attributes</a> that can be set via mass-assignment, such as <tt><a href="Base.html#M000518">new</a>(<a href="Base.html#M000543">attributes</a>)</tt>, <tt><a href="Base.html#M000531">update_attributes</a>(<a href="Base.html#M000543">attributes</a>)</tt>, or <tt><a href="Base.html#M000542">attributes=</a>(<a href="Base.html#M000543">attributes</a>)</tt> </p> <p> This is the opposite of the <tt><a href="Base.html#M000475">attr_protected</a></tt> macro: Mass-assignment will only set <a href="Base.html#M000543">attributes</a> in this list, to assign to the rest of <a href="Base.html#M000543">attributes</a> you can use direct writer methods. This is meant to protect sensitive <a href="Base.html#M000543">attributes</a> from being overwritten by malicious users tampering with URLs or forms. If you‘d rather start from an <a href="Base.html#M000461">all</a>-open default and restrict <a href="Base.html#M000543">attributes</a> as needed, have a look at <tt><a href="Base.html#M000475">attr_protected</a></tt>. </p> <pre> class Customer < ActiveRecord::Base attr_accessible :name, :nickname end customer = Customer.new(:name => "David", :nickname => "Dave", :credit_rating => "Excellent") customer.credit_rating # => nil customer.attributes = { :name => "Jolly fellow", :credit_rating => "Superb" } customer.credit_rating # => nil customer.credit_rating = "Average" customer.credit_rating # => "Average" </pre> </div> </div> <div id="method-M000475" class="method-detail"> <a name="M000475"></a> <div class="method-heading"> <a href="Base.src/M000475.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000475.html');return false;"> <span class="method-name">attr_protected</span><span class="method-args">(*attributes)</span> </a> </div> <div class="method-description"> <p> Attributes named in this macro are protected from mass-assignment, such as <tt><a href="Base.html#M000518">new</a>(<a href="Base.html#M000543">attributes</a>)</tt>, <tt><a href="Base.html#M000531">update_attributes</a>(<a href="Base.html#M000543">attributes</a>)</tt>, or <tt><a href="Base.html#M000542">attributes=</a>(<a href="Base.html#M000543">attributes</a>)</tt>. </p> <p> Mass-assignment to these <a href="Base.html#M000543">attributes</a> will simply be ignored, to assign to them you can use direct writer methods. This is meant to protect sensitive <a href="Base.html#M000543">attributes</a> from being overwritten by malicious users tampering with URLs or forms. </p> <pre> class Customer < ActiveRecord::Base attr_protected :credit_rating end customer = Customer.new("name" => David, "credit_rating" => "Excellent") customer.credit_rating # => nil customer.attributes = { "description" => "Jolly fellow", "credit_rating" => "Superb" } customer.credit_rating # => nil customer.credit_rating = "Average" customer.credit_rating # => "Average" </pre> <p> To start from an <a href="Base.html#M000461">all</a>-closed default and enable <a href="Base.html#M000543">attributes</a> as needed, have a look at <tt><a href="Base.html#M000476">attr_accessible</a></tt>. </p> </div> </div> <div id="method-M000477" class="method-detail"> <a name="M000477"></a> <div class="method-heading"> <a href="Base.src/M000477.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000477.html');return false;"> <span class="method-name">attr_readonly</span><span class="method-args">(*attributes)</span> </a> </div> <div class="method-description"> <p> Attributes listed as readonly can be set for a <a href="Base.html#M000518">new</a> record, but will be ignored in database updates afterwards. </p> </div> </div> <div id="method-M000501" class="method-detail"> <a name="M000501"></a> <div class="method-heading"> <a href="Base.src/M000501.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000501.html');return false;"> <span class="method-name">base_class</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns the base AR subclass that this class descends from. If A extends AR::Base, A.base_class will return A. If B descends from A through some arbitrarily deep hierarchy, B.base_class will return A. </p> </div> </div> <div id="method-M000498" class="method-detail"> <a name="M000498"></a> <div class="method-heading"> <a href="Base.src/M000498.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000498.html');return false;"> <span class="method-name">benchmark</span><span class="method-args">(title, log_level = Logger::DEBUG, use_silence = true) {|| ...}</span> </a> </div> <div class="method-description"> <p> Log and <a href="Base.html#M000498">benchmark</a> multiple statements in a single block. Example: </p> <pre> Project.benchmark("Creating project") do project = Project.create("name" => "stuff") project.create_manager("name" => "David") project.milestones << Milestone.find(:all) end </pre> <p> The <a href="Base.html#M000498">benchmark</a> is only recorded if the current level of the logger is less than or equal to the <tt>log_level</tt>, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the <a href="Base.html#M000498">benchmark</a> will only be conducted if the log level is low enough. </p> <p> The logging of the multiple statements is turned off unless <tt>use_silence</tt> is set to false. </p> </div> </div> <div id="method-M000491" class="method-detail"> <a name="M000491"></a> <div class="method-heading"> <a href="Base.src/M000491.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000491.html');return false;"> <span class="method-name">column_names</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns an array of column names as strings. </p> </div> </div> <div id="method-M000489" class="method-detail"> <a name="M000489"></a> <div class="method-heading"> <a href="Base.src/M000489.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000489.html');return false;"> <span class="method-name">columns</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns an array of column objects for the table associated with this class. </p> </div> </div> <div id="method-M000490" class="method-detail"> <a name="M000490"></a> <div class="method-heading"> <a href="Base.src/M000490.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000490.html');return false;"> <span class="method-name">columns_hash</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a <a href="Base.html#M000552">hash</a> of column objects for the table associated with this class. </p> </div> </div> <div id="method-M000567" class="method-detail"> <a name="M000567"></a> <div class="method-heading"> <a href="Base.src/M000567.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000567.html');return false;"> <span class="method-name">connected?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns true if <tt>ActiveRecord</tt> is connected. </p> </div> </div> <div id="method-M000564" class="method-detail"> <a name="M000564"></a> <div class="method-heading"> <a href="Base.src/M000564.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000564.html');return false;"> <span class="method-name">connection</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns the <a href="Base.html#M000558">connection</a> currently associated with the class. This can also be used to "borrow" the <a href="Base.html#M000558">connection</a> to do database work unrelated to any of the specific Active Records. </p> </div> </div> <div id="method-M000565" class="method-detail"> <a name="M000565"></a> <div class="method-heading"> <a href="Base.src/M000565.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000565.html');return false;"> <span class="method-name">connection_pool</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000492" class="method-detail"> <a name="M000492"></a> <div class="method-heading"> <a href="Base.src/M000492.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000492.html');return false;"> <span class="method-name">content_columns</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns an array of column objects where the primary <a href="Base.html#M000519">id</a>, <a href="Base.html#M000461">all</a> <a href="Base.html#M000489">columns</a> ending in "_id" or "_count", and <a href="Base.html#M000489">columns</a> used for single table inheritance have been removed. </p> </div> </div> <div id="method-M000471" class="method-detail"> <a name="M000471"></a> <div class="method-heading"> <a href="Base.src/M000471.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000471.html');return false;"> <span class="method-name">count_by_sql</span><span class="method-args">(sql)</span> </a> </div> <div class="method-description"> <p> Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part. The use of this method should be restricted to complicated SQL queries that can‘t be executed using the ActiveRecord::Calculations class methods. Look into those before using this. </p> <h4>Parameters</h4> <ul> <li><tt>sql</tt> - An SQL statement which should return a count query from the database, see the example below. </li> </ul> <h4>Examples</h4> <pre> Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id" </pre> </div> </div> <div id="method-M000464" class="method-detail"> <a name="M000464"></a> <div class="method-heading"> <a href="Base.src/M000464.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000464.html');return false;"> <span class="method-name">create</span><span class="method-args">(attributes = nil) {|object| ...}</span> </a> </div> <div class="method-description"> <p> Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not. </p> <p> The <tt><a href="Base.html#M000543">attributes</a></tt> parameter can be either be a Hash or an Array of Hashes. These Hashes describe the <a href="Base.html#M000543">attributes</a> on the objects that are to be created. </p> <h4>Examples</h4> <pre> # Create a single new object User.create(:first_name => 'Jamie') # Create an Array of new objects User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }]) # Create a single object and pass it into a block to set other attributes. User.create(:first_name => 'Jamie') do |u| u.is_admin = false end # Creating an Array of new objects using a block, where the block is executed for each object: User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }]) do |u| u.is_admin = false end </pre> </div> </div> <div id="method-M000474" class="method-detail"> <a name="M000474"></a> <div class="method-heading"> <a href="Base.src/M000474.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000474.html');return false;"> <span class="method-name">decrement_counter</span><span class="method-args">(counter_name, id)</span> </a> </div> <div class="method-description"> <p> Decrement a number field by one, usually representing a count. </p> <p> This works the same as <a href="Base.html#M000473">increment_counter</a> but reduces the column value by 1 instead of increasing it. </p> <h4>Parameters</h4> <ul> <li><tt>counter_name</tt> - The name of the field that should be decremented. </li> <li><tt><a href="Base.html#M000519">id</a></tt> - The <a href="Base.html#M000519">id</a> of the object that should be decremented. </li> </ul> <h4>Examples</h4> <pre> # Decrement the post_count column for the record with an id of 5 DiscussionBoard.decrement_counter(:post_count, 5) </pre> </div> </div> <div id="method-M000466" class="method-detail"> <a name="M000466"></a> <div class="method-heading"> <a href="Base.src/M000466.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000466.html');return false;"> <span class="method-name">delete</span><span class="method-args">(id)</span> </a> </div> <div class="method-description"> <p> Deletes the row with a primary key matching the <tt><a href="Base.html#M000519">id</a></tt> argument, using a SQL <tt>DELETE</tt> statement, and returns the number of rows deleted. Active Record objects are not instantiated, so the object‘s callbacks are not executed, including any <tt>:dependent</tt> association options or <a href="Observer.html">Observer</a> methods. </p> <p> You can <a href="Base.html#M000466">delete</a> multiple rows at once by passing an Array of <tt><a href="Base.html#M000519">id</a></tt>s. </p> <p> Note: Although it is often much faster than the alternative, <tt><a href="Base.html#M000467">destroy</a></tt>, skipping callbacks might bypass business logic in your application that ensures referential integrity or performs other essential jobs. </p> <h4>Examples</h4> <pre> # Delete a single row Todo.delete(1) # Delete multiple rows Todo.delete([2,3,4]) </pre> </div> </div> <div id="method-M000470" class="method-detail"> <a name="M000470"></a> <div class="method-heading"> <a href="Base.src/M000470.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000470.html');return false;"> <span class="method-name">delete_all</span><span class="method-args">(conditions = nil)</span> </a> </div> <div class="method-description"> <p> Deletes the records matching <tt>conditions</tt> without instantiating the records <a href="Base.html#M000459">first</a>, and hence not calling the <tt><a href="Base.html#M000467">destroy</a></tt> method nor invoking callbacks. This is a single SQL DELETE statement that goes straight to the database, much more efficient than <tt><a href="Base.html#M000469">destroy_all</a></tt>. Be careful with relations though, in particular <tt>:dependent</tt> rules defined on associations are not honored. Returns the number of rows affected. </p> <h4>Parameters</h4> <ul> <li><tt>conditions</tt> - Conditions are specified the same way as with <tt><a href="Base.html#M000458">find</a></tt> method. </li> </ul> <h4>Example</h4> <pre> Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')") Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else']) </pre> <p> Both calls <a href="Base.html#M000466">delete</a> the affected posts <a href="Base.html#M000461">all</a> at once with a single DELETE statement. If you need to <a href="Base.html#M000467">destroy</a> dependent associations or call your <tt>before_*</tt> or <tt>after_destroy</tt> callbacks, use the <tt><a href="Base.html#M000469">destroy_all</a></tt> method instead. </p> </div> </div> <div id="method-M000496" class="method-detail"> <a name="M000496"></a> <div class="method-heading"> <a href="Base.src/M000496.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000496.html');return false;"> <span class="method-name">descends_from_active_record?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> True if this isn‘t a concrete subclass needing a STI type condition. </p> </div> </div> <div id="method-M000467" class="method-detail"> <a name="M000467"></a> <div class="method-heading"> <a href="Base.src/M000467.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000467.html');return false;"> <span class="method-name">destroy</span><span class="method-args">(id)</span> </a> </div> <div class="method-description"> <p> Destroy an object (or multiple objects) that has the given <a href="Base.html#M000519">id</a>, the object is instantiated <a href="Base.html#M000459">first</a>, therefore <a href="Base.html#M000461">all</a> callbacks and filters are fired off before the object is deleted. This method is less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run. </p> <p> This essentially finds the object (or multiple objects) with the given <a href="Base.html#M000519">id</a>, creates a <a href="Base.html#M000518">new</a> object from the <a href="Base.html#M000543">attributes</a>, and then calls <a href="Base.html#M000467">destroy</a> on it. </p> <h4>Parameters</h4> <ul> <li><tt><a href="Base.html#M000519">id</a></tt> - Can be either an Integer or an Array of Integers. </li> </ul> <h4>Examples</h4> <pre> # Destroy a single object Todo.destroy(1) # Destroy multiple objects todos = [1,2,3] Todo.destroy(todos) </pre> </div> </div> <div id="method-M000469" class="method-detail"> <a name="M000469"></a> <div class="method-heading"> <a href="Base.src/M000469.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000469.html');return false;"> <span class="method-name">destroy_all</span><span class="method-args">(conditions = nil)</span> </a> </div> <div class="method-description"> <p> Destroys the records matching <tt>conditions</tt> by instantiating each record and calling its <tt><a href="Base.html#M000467">destroy</a></tt> method. Each object‘s callbacks are executed (including <tt>:dependent</tt> association options and <tt>before_destroy</tt>/<tt>after_destroy</tt> <a href="Observer.html">Observer</a> methods). Returns the collection of objects that were destroyed; each will be frozen, to reflect that no changes should be made (since they can‘t be persisted). </p> <p> Note: Instantiation, callback execution, and deletion of each record can be time consuming when you‘re removing many records at once. It generates at least one SQL <tt>DELETE</tt> query per record (or possibly more, to enforce your callbacks). If you want to <a href="Base.html#M000466">delete</a> many rows quickly, without concern for their associations or callbacks, use <tt><a href="Base.html#M000470">delete_all</a></tt> instead. </p> <h4>Parameters</h4> <ul> <li><tt>conditions</tt> - A string, array, or <a href="Base.html#M000552">hash</a> that specifies which records to <a href="Base.html#M000467">destroy</a>. If omitted, <a href="Base.html#M000461">all</a> records are destroyed. See the Conditions section in the introduction to <a href="Base.html">ActiveRecord::Base</a> for more information. </li> </ul> <h4>Examples</h4> <pre> Person.destroy_all("last_login < '2004-04-04'") Person.destroy_all(:status => "inactive") </pre> </div> </div> <div id="method-M000559" class="method-detail"> <a name="M000559"></a> <div class="method-heading"> <a href="Base.src/M000559.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000559.html');return false;"> <span class="method-name">establish_connection</span><span class="method-args">(spec = nil)</span> </a> </div> <div class="method-description"> <p> Establishes the <a href="Base.html#M000558">connection</a> to the database. Accepts a <a href="Base.html#M000552">hash</a> as input where the <tt>:adapter</tt> key must be specified with the name of a database adapter (in lower-case) example for regular databases (MySQL, Postgresql, etc): </p> <pre> ActiveRecord::Base.establish_connection( :adapter => "mysql", :host => "localhost", :username => "myuser", :password => "mypass", :database => "somedatabase" ) </pre> <p> Example for SQLite database: </p> <pre> ActiveRecord::Base.establish_connection( :adapter => "sqlite", :database => "path/to/dbfile" ) </pre> <p> Also accepts keys as strings (for parsing from YAML for example): </p> <pre> ActiveRecord::Base.establish_connection( "adapter" => "sqlite", "database" => "path/to/dbfile" ) </pre> <p> The exceptions <a href="AdapterNotSpecified.html">AdapterNotSpecified</a>, <a href="AdapterNotFound.html">AdapterNotFound</a> and ArgumentError may be returned on an error. </p> </div> </div> <div id="method-M000463" class="method-detail"> <a name="M000463"></a> <div class="method-heading"> <a href="Base.src/M000463.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000463.html');return false;"> <span class="method-name">exists?</span><span class="method-args">(id_or_conditions = {})</span> </a> </div> <div class="method-description"> <p> Returns true if a record exists in the table that matches the <tt><a href="Base.html#M000519">id</a></tt> or conditions given, or false otherwise. The argument can take five forms: </p> <ul> <li>Integer - Finds the record with this primary key. </li> <li>String - Finds the record with a primary key corresponding to this string (such as <tt>‘5‘</tt>). </li> <li>Array - Finds the record that matches these <tt><a href="Base.html#M000458">find</a></tt>-style conditions (such as <tt>[‘color = ?’, ‘red’]</tt>). </li> <li>Hash - Finds the record that matches these <tt><a href="Base.html#M000458">find</a></tt>-style conditions (such as <tt>{:color => ‘red’}</tt>). </li> <li>No args - Returns false if the table is empty, true otherwise. </li> </ul> <p> For more information about specifying conditions as a Hash or Array, see the Conditions section in the introduction to <a href="Base.html">ActiveRecord::Base</a>. </p> <p> Note: You can‘t pass in a condition as a string (like <tt>name = ‘Jamie‘</tt>), since it would be sanitized and then queried against the primary key column, like <tt><a href="Base.html#M000519">id</a> = ‘name = \’Jamie\’‘</tt>. </p> <h4>Examples</h4> <pre> Person.exists?(5) Person.exists?('5') Person.exists?(:name => "David") Person.exists?(['name LIKE ?', "%#{query}%"]) Person.exists? </pre> </div> </div> <div id="method-M000458" class="method-detail"> <a name="M000458"></a> <div class="method-heading"> <a href="Base.src/M000458.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000458.html');return false;"> <span class="method-name">find</span><span class="method-args">(*args)</span> </a> </div> <div class="method-description"> <p> Find operates with four different retrieval approaches: </p> <ul> <li>Find by <a href="Base.html#M000519">id</a> - This can either be a specific <a href="Base.html#M000519">id</a> (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). If no record can be found for <a href="Base.html#M000461">all</a> of the listed ids, then <a href="RecordNotFound.html">RecordNotFound</a> will be raised. </li> <li>Find <a href="Base.html#M000459">first</a> - This will return the <a href="Base.html#M000459">first</a> record matched by the options used. These options can either be specific conditions or merely an order. If no record can be matched, <tt>nil</tt> is returned. Use <tt>Model.find(:<a href="Base.html#M000459">first</a>, *args)</tt> or its shortcut <tt>Model.first(*args)</tt>. </li> <li>Find <a href="Base.html#M000460">last</a> - This will return the <a href="Base.html#M000460">last</a> record matched by the options used. These options can either be specific conditions or merely an order. If no record can be matched, <tt>nil</tt> is returned. Use <tt>Model.find(:<a href="Base.html#M000460">last</a>, *args)</tt> or its shortcut <tt>Model.last(*args)</tt>. </li> <li>Find <a href="Base.html#M000461">all</a> - This will return <a href="Base.html#M000461">all</a> the records matched by the options used. If no records are found, an empty array is returned. Use <tt>Model.find(:<a href="Base.html#M000461">all</a>, *args)</tt> or its shortcut <tt>Model.all(*args)</tt>. </li> </ul> <p> All approaches accept an options <a href="Base.html#M000552">hash</a> as their <a href="Base.html#M000460">last</a> parameter. </p> <h4>Parameters</h4> <ul> <li><tt>:conditions</tt> - An SQL fragment like "administrator = 1", <tt>[ "user_name = ?", username ]</tt>, or <tt>["user_name = :user_name", { :user_name => user_name }]</tt>. See conditions in the intro. </li> <li><tt>:order</tt> - An SQL fragment like "created_at DESC, name". </li> <li><tt>:group</tt> - An attribute name by which the result should be grouped. Uses the <tt>GROUP BY</tt> SQL-clause. </li> <li><tt>:having</tt> - Combined with +:group+ this can be used to filter the records that a <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause. </li> <li><tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned. </li> <li><tt>:offset</tt> - An integer determining the offset from where the rows should be fetched. So at 5, it would skip rows 0 through 4. </li> <li><tt>:joins</tt> - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = <a href="Base.html#M000519">id</a>" (rarely needed), named associations in the same form used for the <tt>:include</tt> option, which will perform an <tt>INNER JOIN</tt> on the associated table(s), or an array containing a mixture of both strings and named associations. If the value is a string, then the records will be returned read-only since they will have <a href="Base.html#M000543">attributes</a> that do not correspond to the table‘s <a href="Base.html#M000489">columns</a>. Pass <tt>:readonly => false</tt> to override. </li> <li><tt>:include</tt> - Names associations that should be loaded alongside. The symbols named refer to already defined associations. See eager loading under Associations. </li> <li><tt>:select</tt> - By default, this is "*" as in "SELECT * FROM", but can be changed if you, for example, want to do a join but not include the joined <a href="Base.html#M000489">columns</a>. Takes a string with the SELECT SQL fragment (e.g. "<a href="Base.html#M000519">id</a>, name"). </li> <li><tt>:from</tt> - By default, this is the table name of the class, but can be changed to an alternate table name (or even the name of a database view). </li> <li><tt>:readonly</tt> - Mark the returned records read-only so they cannot be saved or updated. </li> <li><tt>:lock</tt> - An SQL fragment like "FOR UPDATE" or "LOCK IN SHARE MODE". <tt>:lock => true</tt> gives <a href="Base.html#M000558">connection</a>‘s default exclusive lock, usually "FOR UPDATE". </li> </ul> <h4>Examples</h4> <pre> # find by id Person.find(1) # returns the object for ID = 1 Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6) Person.find([7, 17]) # returns an array for objects with IDs in (7, 17) Person.find([1]) # returns an array for the object with ID = 1 Person.find(1, :conditions => "administrator = 1", :order => "created_on DESC") </pre> <p> Note that returned records may not be in the same order as the ids you provide since database rows are unordered. Give an explicit <tt>:order</tt> to ensure the results are sorted. </p> <h4>Examples</h4> <pre> # find first Person.find(:first) # returns the first object fetched by SELECT * FROM people Person.find(:first, :conditions => [ "user_name = ?", user_name]) Person.find(:first, :conditions => [ "user_name = :u", { :u => user_name }]) Person.find(:first, :order => "created_on DESC", :offset => 5) # find last Person.find(:last) # returns the last object fetched by SELECT * FROM people Person.find(:last, :conditions => [ "user_name = ?", user_name]) Person.find(:last, :order => "created_on DESC", :offset => 5) # find all Person.find(:all) # returns an array of objects for all the rows fetched by SELECT * FROM people Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50) Person.find(:all, :conditions => { :friends => ["Bob", "Steve", "Fred"] } Person.find(:all, :offset => 10, :limit => 10) Person.find(:all, :include => [ :account, :friends ]) Person.find(:all, :group => "category") </pre> <p> Example for <a href="Base.html#M000458">find</a> with a lock: Imagine two concurrent transactions: each will read <tt>person.visits == 2</tt>, add 1 to it, and <a href="Base.html#M000524">save</a>, resulting in two saves of <tt>person.visits = 3</tt>. By locking the row, the second transaction has to wait until the <a href="Base.html#M000459">first</a> is finished; we get the expected <tt>person.visits == 4</tt>. </p> <pre> Person.transaction do person = Person.find(1, :lock => true) person.visits += 1 person.save! end </pre> </div> </div> <div id="method-M000462" class="method-detail"> <a name="M000462"></a> <div class="method-heading"> <a href="Base.src/M000462.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000462.html');return false;"> <span class="method-name">find_by_sql</span><span class="method-args">(sql)</span> </a> </div> <div class="method-description"> <p> Executes a custom SQL query against your database and returns <a href="Base.html#M000461">all</a> the results. The results will be returned as an array with <a href="Base.html#M000489">columns</a> requested encapsulated as <a href="Base.html#M000543">attributes</a> of the model you call this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in a Product object with the <a href="Base.html#M000543">attributes</a> you specified in the SQL query. </p> <p> If you call a complicated SQL query which spans multiple tables the <a href="Base.html#M000489">columns</a> specified by the SELECT will be <a href="Base.html#M000543">attributes</a> of the model, whether or not they are <a href="Base.html#M000489">columns</a> of the corresponding table. </p> <p> The <tt>sql</tt> parameter is a full SQL query as a string. It will be called as is, there will be no database agnostic conversions performed. This should be a <a href="Base.html#M000460">last</a> resort because using, for example, MySQL specific terms will lock you to using that particular database engine or require you to change your call if you switch engines. </p> <h4>Examples</h4> <pre> # A simple SQL query spanning multiple tables Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id" > [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...] # You can use the same string replacement techniques as you can with ActiveRecord#find Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date] > [#<Post:0x36bff9c @attributes={"first_name"=>"The Cheap Man Buys Twice"}>, ...] </pre> </div> </div> <div id="method-M000459" class="method-detail"> <a name="M000459"></a> <div class="method-heading"> <a href="Base.src/M000459.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000459.html');return false;"> <span class="method-name">first</span><span class="method-args">(*args)</span> </a> </div> <div class="method-description"> <p> A convenience wrapper for <tt><a href="Base.html#M000458">find</a>(:<a href="Base.html#M000459">first</a>, *args)</tt>. You can pass in <a href="Base.html#M000461">all</a> the same arguments to this method as you can to <tt><a href="Base.html#M000458">find</a>(:<a href="Base.html#M000459">first</a>)</tt>. </p> </div> </div> <div id="method-M000494" class="method-detail"> <a name="M000494"></a> <div class="method-heading"> <a href="Base.src/M000494.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000494.html');return false;"> <span class="method-name">human_attribute_name</span><span class="method-args">(attribute_key_name, options = {})</span> </a> </div> <div class="method-description"> <p> Transforms attribute key names into a more humane format, such as "First name" instead of "first_name". Example: </p> <pre> Person.human_attribute_name("first_name") # => "First name" </pre> <p> This used to be depricated in favor of humanize, but is now preferred, because it automatically uses the <a href="../I18n.html">I18n</a> module now. Specify <tt>options</tt> with additional translating options. </p> </div> </div> <div id="method-M000495" class="method-detail"> <a name="M000495"></a> <div class="method-heading"> <a href="Base.src/M000495.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000495.html');return false;"> <span class="method-name">human_name</span><span class="method-args">(options = {})</span> </a> </div> <div class="method-description"> <p> Transform the modelname into a more humane format, using <a href="../I18n.html">I18n</a>. Defaults to the basic humanize method. Default scope of the translation is activerecord.models Specify <tt>options</tt> with additional translating options. </p> </div> </div> <div id="method-M000473" class="method-detail"> <a name="M000473"></a> <div class="method-heading"> <a href="Base.src/M000473.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000473.html');return false;"> <span class="method-name">increment_counter</span><span class="method-args">(counter_name, id)</span> </a> </div> <div class="method-description"> <p> Increment a number field by one, usually representing a count. </p> <p> This is used for caching aggregate values, so that they don‘t need to be computed every time. For example, a DiscussionBoard may cache post_count and comment_count otherwise every time the board is shown it would have to run an SQL query to <a href="Base.html#M000458">find</a> how many posts and comments there are. </p> <h4>Parameters</h4> <ul> <li><tt>counter_name</tt> - The name of the field that should be incremented. </li> <li><tt><a href="Base.html#M000519">id</a></tt> - The <a href="Base.html#M000519">id</a> of the object that should be incremented. </li> </ul> <h4>Examples</h4> <pre> # Increment the post_count column for the record with an id of 5 DiscussionBoard.increment_counter(:post_count, 5) </pre> </div> </div> <div id="method-M000483" class="method-detail"> <a name="M000483"></a> <div class="method-heading"> <a href="Base.src/M000483.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000483.html');return false;"> <span class="method-name">inheritance_column</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Defines the column name for use with single table inheritance — can be set in subclasses like so: self.inheritance_column = "type_id" </p> </div> </div> <div id="method-M000497" class="method-detail"> <a name="M000497"></a> <div class="method-heading"> <a href="Base.src/M000497.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000497.html');return false;"> <span class="method-name">inspect</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a string like ‘Post <a href="Base.html#M000519">id</a>:integer, title:string, body:text‘ </p> </div> </div> <div id="method-M000460" class="method-detail"> <a name="M000460"></a> <div class="method-heading"> <a href="Base.src/M000460.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000460.html');return false;"> <span class="method-name">last</span><span class="method-args">(*args)</span> </a> </div> <div class="method-description"> <p> A convenience wrapper for <tt><a href="Base.html#M000458">find</a>(:<a href="Base.html#M000460">last</a>, *args)</tt>. You can pass in <a href="Base.html#M000461">all</a> the same arguments to this method as you can to <tt><a href="Base.html#M000458">find</a>(:<a href="Base.html#M000460">last</a>)</tt>. </p> </div> </div> <div id="method-M000505" class="method-detail"> <a name="M000505"></a> <div class="method-heading"> <a href="Base.src/M000505.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000505.html');return false;"> <span class="method-name">merge_conditions</span><span class="method-args">(*conditions)</span> </a> </div> <div class="method-description"> <p> Merges conditions so that the result is a valid <tt>condition</tt> </p> </div> </div> <div id="method-M000518" class="method-detail"> <a name="M000518"></a> <div class="method-heading"> <a href="Base.src/M000518.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000518.html');return false;"> <span class="method-name">new</span><span class="method-args">(attributes = nil) {|self if block_given?| ...}</span> </a> </div> <div class="method-description"> <p> New objects can be instantiated as either empty (pass no construction parameter) or pre-set with <a href="Base.html#M000543">attributes</a> but not yet saved (pass a <a href="Base.html#M000552">hash</a> with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table — hence you can‘t have <a href="Base.html#M000543">attributes</a> that aren‘t part of the table <a href="Base.html#M000489">columns</a>. </p> </div> </div> <div id="method-M000482" class="method-detail"> <a name="M000482"></a> <div class="method-heading"> <a href="Base.src/M000482.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000482.html');return false;"> <span class="method-name">primary_key</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Defines the primary key field — can be overridden in subclasses. Overwriting will negate any effect of the primary_key_prefix_type setting, though. </p> </div> </div> <div id="method-M000478" class="method-detail"> <a name="M000478"></a> <div class="method-heading"> <a href="Base.src/M000478.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000478.html');return false;"> <span class="method-name">readonly_attributes</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns an array of <a href="Base.html#M000461">all</a> the <a href="Base.html#M000543">attributes</a> that have been specified as readonly. </p> </div> </div> <div id="method-M000568" class="method-detail"> <a name="M000568"></a> <div class="method-heading"> <a href="Base.src/M000568.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000568.html');return false;"> <span class="method-name">remove_connection</span><span class="method-args">(klass = self)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000493" class="method-detail"> <a name="M000493"></a> <div class="method-heading"> <a href="Base.src/M000493.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000493.html');return false;"> <span class="method-name">reset_column_information</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Resets <a href="Base.html#M000461">all</a> the cached information about <a href="Base.html#M000489">columns</a>, which will cause them to be reloaded on the next request. </p> <p> The most common usage pattern for this method is probably in a migration, when just after creating a table you want to populate it with some default values, eg: </p> <pre> class CreateJobLevels < ActiveRecord::Migration def self.up create_table :job_levels do |t| t.integer :id t.string :name t.timestamps end JobLevel.reset_column_information %w{assistant executive manager director}.each do |type| JobLevel.create(:name => type) end end def self.down drop_table :job_levels end end </pre> </div> </div> <div id="method-M000503" class="method-detail"> <a name="M000503"></a> <div class="method-heading"> <a href="Base.src/M000503.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000503.html');return false;"> <span class="method-name">respond_to?</span><span class="method-args">(method_id, include_private = false)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000566" class="method-detail"> <a name="M000566"></a> <div class="method-heading"> <a href="Base.src/M000566.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000566.html');return false;"> <span class="method-name">retrieve_connection</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000479" class="method-detail"> <a name="M000479"></a> <div class="method-heading"> <a href="Base.src/M000479.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000479.html');return false;"> <span class="method-name">serialize</span><span class="method-args">(attr_name, class_name = Object)</span> </a> </div> <div class="method-description"> <p> If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object, then specify the name of that attribute using this method and it will be handled automatically. The serialization is done through YAML. If <tt>class_name</tt> is specified, the serialized object must be of that class on retrieval or <a href="SerializationTypeMismatch.html">SerializationTypeMismatch</a> will be raised. </p> <h4>Parameters</h4> <ul> <li><tt>attr_name</tt> - The field name that should be serialized. </li> <li><tt>class_name</tt> - Optional, class name that the object type should be equal to. </li> </ul> <h4>Example</h4> <pre> # Serialize a preferences attribute class User serialize :preferences end </pre> </div> </div> <div id="method-M000480" class="method-detail"> <a name="M000480"></a> <div class="method-heading"> <a href="Base.src/M000480.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000480.html');return false;"> <span class="method-name">serialized_attributes</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a <a href="Base.html#M000552">hash</a> of <a href="Base.html#M000461">all</a> the <a href="Base.html#M000543">attributes</a> that have been specified for serialization as keys and their class restriction as values. </p> </div> </div> <div id="method-M000486" class="method-detail"> <a name="M000486"></a> <div class="method-heading"> <a href="Base.src/M000486.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000486.html');return false;"> <span class="method-name">set_inheritance_column</span><span class="method-args">(value = nil, &block)</span> </a> </div> <div class="method-description"> <p> Sets the name of the inheritance column to use to the given value, or (if the value # is nil or false) to the value returned by the given block. </p> <pre> class Project < ActiveRecord::Base set_inheritance_column do original_inheritance_column + "_id" end end </pre> </div> </div> <div id="method-M000485" class="method-detail"> <a name="M000485"></a> <div class="method-heading"> <a href="Base.src/M000485.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000485.html');return false;"> <span class="method-name">set_primary_key</span><span class="method-args">(value = nil, &block)</span> </a> </div> <div class="method-description"> <p> Sets the name of the primary key column to use to the given value, or (if the value is nil or false) to the value returned by the given block. </p> <pre> class Project < ActiveRecord::Base set_primary_key "sysid" end </pre> </div> </div> <div id="method-M000487" class="method-detail"> <a name="M000487"></a> <div class="method-heading"> <a href="Base.src/M000487.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000487.html');return false;"> <span class="method-name">set_sequence_name</span><span class="method-args">(value = nil, &block)</span> </a> </div> <div class="method-description"> <p> Sets the name of the sequence to use when generating ids to the given value, or (if the value is nil or false) to the value returned by the given block. This is required for Oracle and is useful for any database which relies on sequences for primary key generation. </p> <p> If a sequence name is not explicitly set when using Oracle or Firebird, it will default to the commonly used pattern of: #{<a href="Base.html#M000481">table_name</a>}_seq </p> <p> If a sequence name is not explicitly set when using PostgreSQL, it will discover the sequence corresponding to your primary key for you. </p> <pre> class Project < ActiveRecord::Base set_sequence_name "projectseq" # default would have been "project_seq" end </pre> </div> </div> <div id="method-M000484" class="method-detail"> <a name="M000484"></a> <div class="method-heading"> <a href="Base.src/M000484.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000484.html');return false;"> <span class="method-name">set_table_name</span><span class="method-args">(value = nil, &block)</span> </a> </div> <div class="method-description"> <p> Sets the table name to use to the given value, or (if the value is nil or false) to the value returned by the given block. </p> <pre> class Project < ActiveRecord::Base set_table_name "project" end </pre> </div> </div> <div id="method-M000499" class="method-detail"> <a name="M000499"></a> <div class="method-heading"> <a href="Base.src/M000499.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000499.html');return false;"> <span class="method-name">silence</span><span class="method-args">() {|| ...}</span> </a> </div> <div class="method-description"> <p> Silences the logger for the duration of the block. </p> </div> </div> <div id="method-M000504" class="method-detail"> <a name="M000504"></a> <div class="method-heading"> <a href="Base.src/M000504.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000504.html');return false;"> <span class="method-name">sti_name</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000488" class="method-detail"> <a name="M000488"></a> <div class="method-heading"> <a href="Base.src/M000488.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000488.html');return false;"> <span class="method-name">table_exists?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Indicates whether the table associated with this class exists </p> </div> </div> <div id="method-M000481" class="method-detail"> <a name="M000481"></a> <div class="method-heading"> <a href="Base.src/M000481.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000481.html');return false;"> <span class="method-name">table_name</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from <a href="Base.html">ActiveRecord::Base</a>. So if the hierarchy looks like: Reply < Message < <a href="Base.html">ActiveRecord::Base</a>, then Message is used to guess the table name even when called on Reply. The rules used to do the guess are handled by the Inflector class in Active Support, which knows almost <a href="Base.html#M000461">all</a> common English inflections. You can add <a href="Base.html#M000518">new</a> inflections in config/initializers/inflections.rb. </p> <p> Nested classes are given table names prefixed by the singular form of the parent‘s table name. Enclosing modules are not considered. </p> <h4>Examples</h4> <pre> class Invoice < ActiveRecord::Base; end; file class table_name invoice.rb Invoice invoices class Invoice < ActiveRecord::Base; class Lineitem < ActiveRecord::Base; end; end; file class table_name invoice.rb Invoice::Lineitem invoice_lineitems module Invoice; class Lineitem < ActiveRecord::Base; end; end; file class table_name invoice/lineitem.rb Invoice::Lineitem lineitems </pre> <p> Additionally, the class-level <tt>table_name_prefix</tt> is prepended and the <tt>table_name_suffix</tt> is appended. So if you have "myapp_" as a prefix, the table name guess for an Invoice class <a href="Base.html#M000529">becomes</a> "myapp_invoices". Invoice::Lineitem <a href="Base.html#M000529">becomes</a> "myapp_invoice_lineitems". </p> <p> You can also overwrite this class method to allow for unguessable links, such as a Mouse class with a link to a "mice" table. Example: </p> <pre> class Mouse < ActiveRecord::Base set_table_name "mice" end </pre> </div> </div> <div id="method-M000465" class="method-detail"> <a name="M000465"></a> <div class="method-heading"> <a href="Base.src/M000465.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000465.html');return false;"> <span class="method-name">update</span><span class="method-args">(id, attributes)</span> </a> </div> <div class="method-description"> <p> Updates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not. </p> <h4>Parameters</h4> <ul> <li><tt><a href="Base.html#M000519">id</a></tt> - This should be the <a href="Base.html#M000519">id</a> or an array of ids to be updated. </li> <li><tt><a href="Base.html#M000543">attributes</a></tt> - This should be a <a href="Base.html#M000552">hash</a> of <a href="Base.html#M000543">attributes</a> to be set on the object, or an array of hashes. </li> </ul> <h4>Examples</h4> <pre> # Updating one record: Person.update(15, :user_name => 'Samuel', :group => 'expert') # Updating multiple records: people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } } Person.update(people.keys, people.values) </pre> </div> </div> <div id="method-M000468" class="method-detail"> <a name="M000468"></a> <div class="method-heading"> <a href="Base.src/M000468.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000468.html');return false;"> <span class="method-name">update_all</span><span class="method-args">(updates, conditions = nil, options = {})</span> </a> </div> <div class="method-description"> <p> Updates <a href="Base.html#M000461">all</a> records with details given if they match a set of conditions supplied, limits and order can also be supplied. This method constructs a single SQL UPDATE statement and sends it straight to the database. It does not instantiate the involved models and it does not trigger Active Record callbacks. </p> <h4>Parameters</h4> <ul> <li><tt>updates</tt> - A string of column and value pairs that will be set on any records that match conditions. This creates the SET clause of the generated SQL. </li> <li><tt>conditions</tt> - An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro for more info. </li> <li><tt>options</tt> - Additional options are <tt>:limit</tt> and <tt>:order</tt>, see the examples for usage. </li> </ul> <h4>Examples</h4> <pre> # Update all billing objects with the 3 different attributes given Billing.update_all( "category = 'authorized', approved = 1, author = 'David'" ) # Update records that match our conditions Billing.update_all( "author = 'David'", "title LIKE '%Rails%'" ) # Update records that match our conditions but limit it to 5 ordered by date Billing.update_all( "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 ) </pre> </div> </div> <div id="method-M000472" class="method-detail"> <a name="M000472"></a> <div class="method-heading"> <a href="Base.src/M000472.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000472.html');return false;"> <span class="method-name">update_counters</span><span class="method-args">(id, counters)</span> </a> </div> <div class="method-description"> <p> A generic "counter updater" implementation, intended primarily to be used by <a href="Base.html#M000473">increment_counter</a> and <a href="Base.html#M000474">decrement_counter</a>, but which may also be useful on its own. It simply does a direct SQL <a href="Base.html#M000465">update</a> for the record with the given ID, altering the given <a href="Base.html#M000552">hash</a> of counters by the amount given by the corresponding value: </p> <h4>Parameters</h4> <ul> <li><tt><a href="Base.html#M000519">id</a></tt> - The <a href="Base.html#M000519">id</a> of the object you wish to <a href="Base.html#M000465">update</a> a counter on or an Array of ids. </li> <li><tt>counters</tt> - An Array of Hashes containing the names of the fields to <a href="Base.html#M000465">update</a> as keys and the amount to <a href="Base.html#M000465">update</a> the field by as values. </li> </ul> <h4>Examples</h4> <pre> # For the Post with id of 5, decrement the comment_count by 1, and # increment the action_count by 1 Post.update_counters 5, :comment_count => -1, :action_count => 1 # Executes the following SQL: # UPDATE posts # SET comment_count = comment_count - 1, # action_count = action_count + 1 # WHERE id = 5 # For the Posts with id of 10 and 15, increment the comment_count by 1 Post.update_counters [10, 15], :comment_count => 1 # Executes the following SQL: # UPDATE posts # SET comment_count = comment_count + 1, # WHERE id IN (10, 15) </pre> </div> </div> <div id="method-M000562" class="method-detail"> <a name="M000562"></a> <div class="method-heading"> <a href="Base.src/M000562.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000562.html');return false;"> <span class="method-name">verification_timeout</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Deprecated and no longer has any effect. </p> </div> </div> <div id="method-M000563" class="method-detail"> <a name="M000563"></a> <div class="method-heading"> <a href="Base.src/M000563.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000563.html');return false;"> <span class="method-name">verification_timeout=</span><span class="method-args">(flag)</span> </a> </div> <div class="method-description"> <p> Deprecated and no longer has any effect. </p> </div> </div> <h3 class="section-bar">Protected Class methods</h3> <div id="method-M000513" class="method-detail"> <a name="M000513"></a> <div class="method-heading"> <a href="Base.src/M000513.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000513.html');return false;"> <span class="method-name">aggregate_mapping</span><span class="method-args">(reflection)</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000510" class="method-detail"> <a name="M000510"></a> <div class="method-heading"> <a href="Base.src/M000510.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000510.html');return false;"> <span class="method-name">class_of_active_record_descendant</span><span class="method-args">(klass)</span> </a> </div> <div class="method-description"> <p> Returns the class descending directly from <a href="Base.html">ActiveRecord::Base</a> or an abstract class, if any, in the inheritance hierarchy. </p> </div> </div> <div id="method-M000509" class="method-detail"> <a name="M000509"></a> <div class="method-heading"> <a href="Base.src/M000509.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000509.html');return false;"> <span class="method-name">compute_type</span><span class="method-args">(type_name)</span> </a> </div> <div class="method-description"> <p> Returns the class type of the record using the current module as a prefix. So descendants of MyApp::Business::Account would appear as MyApp::Business::AccountSubclass. </p> </div> </div> <div id="method-M000508" class="method-detail"> <a name="M000508"></a> <div class="method-heading"> <a href="Base.src/M000508.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000508.html');return false;"> <span class="method-name">default_scope</span><span class="method-args">(options = {})</span> </a> </div> <div class="method-description"> <p> Sets the default options for the model. The format of the <tt>options</tt> argument is the same as in <a href="Base.html#M000458">find</a>. </p> <pre> class Person < ActiveRecord::Base default_scope :order => 'last_name, first_name' end </pre> </div> </div> <div id="method-M000514" class="method-detail"> <a name="M000514"></a> <div class="method-heading"> <a href="Base.src/M000514.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000514.html');return false;"> <span class="method-name">expand_hash_conditions_for_aggregates</span><span class="method-args">(attrs)</span> </a> </div> <div class="method-description"> <p> Accepts a <a href="Base.html#M000552">hash</a> of SQL conditions and replaces those <a href="Base.html#M000543">attributes</a> that correspond to a <tt>composed_of</tt> relationship with their expanded aggregate attribute values. Given: </p> <pre> class Person < ActiveRecord::Base composed_of :address, :class_name => "Address", :mapping => [%w(address_street street), %w(address_city city)] end </pre> <p> Then: </p> <pre> { :address => Address.new("813 abc st.", "chicago") } # => { :address_street => "813 abc st.", :address_city => "chicago" } </pre> </div> </div> <div id="method-M000517" class="method-detail"> <a name="M000517"></a> <div class="method-heading"> <a href="Base.src/M000517.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000517.html');return false;"> <span class="method-name">sanitize_sql_array</span><span class="method-args">(ary)</span> </a> </div> <div class="method-description"> <p> Accepts an array of conditions. The array has each value sanitized and interpolated into the SQL statement. </p> <pre> ["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'" </pre> </div> </div> <div id="method-M000512" class="method-detail"> <a name="M000512"></a> <div class="method-heading"> <a href="Base.src/M000512.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000512.html');return false;"> <span class="method-name">sanitize_sql_for_assignment</span><span class="method-args">(assignments)</span> </a> </div> <div class="method-description"> <p> Accepts an array, <a href="Base.html#M000552">hash</a>, or string of SQL conditions and sanitizes them into a valid SQL fragment for a SET clause. </p> <pre> { :name => nil, :group_id => 4 } returns "name = NULL , group_id='4'" </pre> </div> </div> <div id="method-M000511" class="method-detail"> <a name="M000511"></a> <div class="method-heading"> <a href="Base.src/M000511.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000511.html');return false;"> <span class="method-name">sanitize_sql_for_conditions</span><span class="method-args">(condition, table_name = quoted_table_name)</span> </a> </div> <div class="method-description"> <p> Accepts an array, <a href="Base.html#M000552">hash</a>, or string of SQL conditions and sanitizes them into a valid SQL fragment for a WHERE clause. </p> <pre> ["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'" { :name => "foo'bar", :group_id => 4 } returns "name='foo''bar' and group_id='4'" "name='foo''bar' and group_id='4'" returns "name='foo''bar' and group_id='4'" </pre> </div> </div> <div id="method-M000516" class="method-detail"> <a name="M000516"></a> <div class="method-heading"> <a href="Base.src/M000516.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000516.html');return false;"> <span class="method-name">sanitize_sql_hash_for_assignment</span><span class="method-args">(attrs)</span> </a> </div> <div class="method-description"> <p> Sanitizes a <a href="Base.html#M000552">hash</a> of attribute/value pairs into SQL conditions for a SET clause. </p> <pre> { :status => nil, :group_id => 1 } # => "status = NULL , group_id = 1" </pre> </div> </div> <div id="method-M000515" class="method-detail"> <a name="M000515"></a> <div class="method-heading"> <a href="Base.src/M000515.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000515.html');return false;"> <span class="method-name">sanitize_sql_hash_for_conditions</span><span class="method-args">(attrs, default_table_name = quoted_table_name)</span> </a> </div> <div class="method-description"> <p> Sanitizes a <a href="Base.html#M000552">hash</a> of attribute/value pairs into SQL conditions for a WHERE clause. </p> <pre> { :name => "foo'bar", :group_id => 4 } # => "name='foo''bar' and group_id= 4" { :status => nil, :group_id => [1,2,3] } # => "status IS NULL and group_id IN (1,2,3)" { :age => 13..18 } # => "age BETWEEN 13 AND 18" { 'other_records.id' => 7 } # => "`other_records`.`id` = 7" { :other_records => { :id => 7 } } # => "`other_records`.`id` = 7" </pre> <p> And for value objects on a composed_of relationship: </p> <pre> { :address => Address.new("123 abc st.", "chicago") } # => "address_street='123 abc st.' and address_city='chicago'" </pre> </div> </div> <div id="method-M000507" class="method-detail"> <a name="M000507"></a> <div class="method-heading"> <a href="Base.src/M000507.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000507.html');return false;"> <span class="method-name">with_exclusive_scope</span><span class="method-args">(method_scoping = {}, &block)</span> </a> </div> <div class="method-description"> <p> Works like <a href="Base.html#M000506">with_scope</a>, but discards any nested properties. </p> </div> </div> <div id="method-M000506" class="method-detail"> <a name="M000506"></a> <div class="method-heading"> <a href="Base.src/M000506.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000506.html');return false;"> <span class="method-name">with_scope</span><span class="method-args">(method_scoping = {}, action = :merge) {|| ...}</span> </a> </div> <div class="method-description"> <p> Scope parameters to method calls within the block. Takes a <a href="Base.html#M000552">hash</a> of method_name => parameters <a href="Base.html#M000552">hash</a>. method_name may be <tt>:<a href="Base.html#M000458">find</a></tt> or <tt>:<a href="Base.html#M000464">create</a></tt>. <tt>:<a href="Base.html#M000458">find</a></tt> parameters may include the <tt>:conditions</tt>, <tt>:joins</tt>, <tt>:include</tt>, <tt>:offset</tt>, <tt>:limit</tt>, and <tt>:readonly</tt> options. <tt>:<a href="Base.html#M000464">create</a></tt> parameters are an <a href="Base.html#M000543">attributes</a> <a href="Base.html#M000552">hash</a>. </p> <pre> class Article < ActiveRecord::Base def self.create_with_scope with_scope(:find => { :conditions => "blog_id = 1" }, :create => { :blog_id => 1 }) do find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1 a = create(1) a.blog_id # => 1 end end end </pre> <p> In nested scopings, <a href="Base.html#M000461">all</a> previous parameters are overwritten by the innermost rule, with the exception of <tt>:conditions</tt>, <tt>:include</tt>, and <tt>:joins</tt> options in <tt>:<a href="Base.html#M000458">find</a></tt>, which are merged. </p> <p> <tt>:joins</tt> options are uniqued so multiple scopes can join in the same table without table aliasing problems. If you need to join multiple tables, but still want one of the tables to be uniqued, use the array of strings format for your joins. </p> <pre> class Article < ActiveRecord::Base def self.find_with_scope with_scope(:find => { :conditions => "blog_id = 1", :limit => 1 }, :create => { :blog_id => 1 }) do with_scope(:find => { :limit => 10 }) find(:all) # => SELECT * from articles WHERE blog_id = 1 LIMIT 10 end with_scope(:find => { :conditions => "author_id = 3" }) find(:all) # => SELECT * from articles WHERE blog_id = 1 AND author_id = 3 LIMIT 1 end end end end </pre> <p> You can ignore any previous scopings by using the <tt><a href="Base.html#M000507">with_exclusive_scope</a></tt> method. </p> <pre> class Article < ActiveRecord::Base def self.find_with_exclusive_scope with_scope(:find => { :conditions => "blog_id = 1", :limit => 1 }) do with_exclusive_scope(:find => { :limit => 10 }) find(:all) # => SELECT * from articles LIMIT 10 end end end end </pre> <p> <b>Note</b>: the +:<a href="Base.html#M000458">find</a>+ scope also has effect on <a href="Base.html#M000465">update</a> and deletion methods, like <tt><a href="Base.html#M000468">update_all</a></tt> and <tt><a href="Base.html#M000470">delete_all</a></tt>. </p> </div> </div> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000550" class="method-detail"> <a name="M000550"></a> <div class="method-heading"> <a href="Base.src/M000550.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000550.html');return false;"> <span class="method-name">==</span><span class="method-args">(comparison_object)</span> </a> </div> <div class="method-description"> <p> Returns true if the <tt>comparison_object</tt> is the same object, or is of the same type and has the same <a href="Base.html#M000519">id</a>. </p> </div> </div> <div id="method-M000540" class="method-detail"> <a name="M000540"></a> <div class="method-heading"> <a href="Base.src/M000540.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000540.html');return false;"> <span class="method-name">[]</span><span class="method-args">(attr_name)</span> </a> </div> <div class="method-description"> <p> Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example, "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). (Alias for the protected read_attribute method). </p> </div> </div> <div id="method-M000541" class="method-detail"> <a name="M000541"></a> <div class="method-heading"> <a href="Base.src/M000541.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000541.html');return false;"> <span class="method-name">[]=</span><span class="method-args">(attr_name, value)</span> </a> </div> <div class="method-description"> <p> Updates the attribute identified by <tt>attr_name</tt> with the specified <tt>value</tt>. (Alias for the protected write_attribute method). </p> </div> </div> <div id="method-M000545" class="method-detail"> <a name="M000545"></a> <div class="method-heading"> <a href="Base.src/M000545.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000545.html');return false;"> <span class="method-name">attribute_for_inspect</span><span class="method-args">(attr_name)</span> </a> </div> <div class="method-description"> <p> Returns an <tt><a href="Base.html#M000497">inspect</a></tt>-like string for the value of the attribute <tt>attr_name</tt>. String <a href="Base.html#M000543">attributes</a> are elided after 50 characters, and Date and Time <a href="Base.html#M000543">attributes</a> are returned in the <tt>:db</tt> format. Other <a href="Base.html#M000543">attributes</a> return the value of <tt><a href="Base.html#M000497">inspect</a></tt> without modification. </p> <pre> person = Person.create!(:name => "David Heinemeier Hansson " * 3) person.attribute_for_inspect(:name) # => '"David Heinemeier Hansson David Heinemeier Hansson D..."' person.attribute_for_inspect(:created_at) # => '"2009-01-12 04:48:57"' </pre> </div> </div> <div id="method-M000548" class="method-detail"> <a name="M000548"></a> <div class="method-heading"> <a href="Base.src/M000548.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000548.html');return false;"> <span class="method-name">attribute_names</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns an array of names for the <a href="Base.html#M000543">attributes</a> available on this object sorted alphabetically. </p> </div> </div> <div id="method-M000546" class="method-detail"> <a name="M000546"></a> <div class="method-heading"> <a href="Base.src/M000546.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000546.html');return false;"> <span class="method-name">attribute_present?</span><span class="method-args">(attribute)</span> </a> </div> <div class="method-description"> <p> Returns true if the specified <tt>attribute</tt> has been set by the user or by a database load and is neither nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings). </p> </div> </div> <div id="method-M000543" class="method-detail"> <a name="M000543"></a> <div class="method-heading"> <a href="Base.src/M000543.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000543.html');return false;"> <span class="method-name">attributes</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a <a href="Base.html#M000552">hash</a> of <a href="Base.html#M000461">all</a> the <a href="Base.html#M000543">attributes</a> with their names as keys and the values of the <a href="Base.html#M000543">attributes</a> as values. </p> </div> </div> <div id="method-M000542" class="method-detail"> <a name="M000542"></a> <div class="method-heading"> <a href="Base.src/M000542.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000542.html');return false;"> <span class="method-name">attributes=</span><span class="method-args">(new_attributes, guard_protected_attributes = true)</span> </a> </div> <div class="method-description"> <p> Allows you to set <a href="Base.html#M000461">all</a> the <a href="Base.html#M000543">attributes</a> at once by passing in a <a href="Base.html#M000552">hash</a> with keys matching the attribute names (which again matches the column names). </p> <p> If <tt>guard_protected_attributes</tt> is true (the default), then sensitive <a href="Base.html#M000543">attributes</a> can be protected from this form of mass-assignment by using the <tt><a href="Base.html#M000475">attr_protected</a></tt> macro. Or you can alternatively specify which <a href="Base.html#M000543">attributes</a> <b>can</b> be accessed with the <tt><a href="Base.html#M000476">attr_accessible</a></tt> macro. Then <a href="Base.html#M000461">all</a> the <a href="Base.html#M000543">attributes</a> not included in that won‘t be allowed to be mass-assigned. </p> <pre> class User < ActiveRecord::Base attr_protected :is_admin end user = User.new user.attributes = { :username => 'Phusion', :is_admin => true } user.username # => "Phusion" user.is_admin? # => false user.send(:attributes=, { :username => 'Phusion', :is_admin => true }, false) user.is_admin? # => true </pre> </div> </div> <div id="method-M000544" class="method-detail"> <a name="M000544"></a> <div class="method-heading"> <a href="Base.src/M000544.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000544.html');return false;"> <span class="method-name">attributes_before_type_cast</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a <a href="Base.html#M000552">hash</a> of <a href="Base.html#M000543">attributes</a> before typecasting and deserialization. </p> </div> </div> <div id="method-M000529" class="method-detail"> <a name="M000529"></a> <div class="method-heading"> <a href="Base.src/M000529.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000529.html');return false;"> <span class="method-name">becomes</span><span class="method-args">(klass)</span> </a> </div> <div class="method-description"> <p> Returns an instance of the specified <tt>klass</tt> with the <a href="Base.html#M000543">attributes</a> of the current record. This is mostly useful in relation to single-table inheritance structures where you want a subclass to appear as the superclass. This can be used along with record identification in Action Pack to allow, say, <tt>Client < Company</tt> to do something like render <tt>:partial => @client.becomes(Company)</tt> to render that instance using the companies/company partial instead of clients/client. </p> <p> Note: The <a href="Base.html#M000518">new</a> instance will share a link to the same <a href="Base.html#M000543">attributes</a> as the original class. So any change to the <a href="Base.html#M000543">attributes</a> in either instance will affect the other. </p> </div> </div> <div id="method-M000521" class="method-detail"> <a name="M000521"></a> <div class="method-heading"> <a href="Base.src/M000521.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000521.html');return false;"> <span class="method-name">cache_key</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a cache key that can be used to identify this record. </p> <h4>Examples</h4> <pre> Product.new.cache_key # => "products/new" Product.find(5).cache_key # => "products/5" (updated_at not available) Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available) </pre> </div> </div> <div id="method-M000528" class="method-detail"> <a name="M000528"></a> <div class="method-heading"> <a href="Base.src/M000528.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000528.html');return false;"> <span class="method-name">clone</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a <a href="Base.html#M000528">clone</a> of the record that hasn‘t been assigned an <a href="Base.html#M000519">id</a> yet and is treated as a <a href="Base.html#M000518">new</a> record. Note that this is a "shallow" <a href="Base.html#M000528">clone</a>: it copies the object‘s <a href="Base.html#M000543">attributes</a> only, not its associations. The extent of a "deep" <a href="Base.html#M000528">clone</a> is application-specific and is therefore left to the application to implement according to its need. </p> </div> </div> <div id="method-M000549" class="method-detail"> <a name="M000549"></a> <div class="method-heading"> <a href="Base.src/M000549.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000549.html');return false;"> <span class="method-name">column_for_attribute</span><span class="method-args">(name)</span> </a> </div> <div class="method-description"> <p> Returns the column object for the named attribute. </p> </div> </div> <div id="method-M000558" class="method-detail"> <a name="M000558"></a> <div class="method-heading"> <a href="Base.src/M000558.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000558.html');return false;"> <span class="method-name">connection</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns the <a href="Base.html#M000558">connection</a> currently associated with the class. This can also be used to "borrow" the <a href="Base.html#M000558">connection</a> to do database work that isn‘t easily done without going straight to SQL. </p> </div> </div> <div id="method-M000535" class="method-detail"> <a name="M000535"></a> <div class="method-heading"> <a href="Base.src/M000535.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000535.html');return false;"> <span class="method-name">decrement</span><span class="method-args">(attribute, by = 1)</span> </a> </div> <div class="method-description"> <p> Initializes <tt>attribute</tt> to zero if <tt>nil</tt> and subtracts the value passed as <tt>by</tt> (default is 1). The <a href="Base.html#M000535">decrement</a> is performed directly on the underlying attribute, no setter is invoked. Only makes sense for number-based <a href="Base.html#M000543">attributes</a>. Returns <tt>self</tt>. </p> </div> </div> <div id="method-M000536" class="method-detail"> <a name="M000536"></a> <div class="method-heading"> <a href="Base.src/M000536.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000536.html');return false;"> <span class="method-name">decrement!</span><span class="method-args">(attribute, by = 1)</span> </a> </div> <div class="method-description"> <p> Wrapper around <tt><a href="Base.html#M000535">decrement</a></tt> that saves the record. This method differs from its non-bang version in that it passes through the attribute setter. Saving is not subjected to validation checks. Returns <tt>true</tt> if the record could be saved. </p> </div> </div> <div id="method-M000526" class="method-detail"> <a name="M000526"></a> <div class="method-heading"> <a href="Base.src/M000526.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000526.html');return false;"> <span class="method-name">delete</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can‘t be persisted). Returns the frozen instance. </p> <p> The row is simply removed with a SQL <tt>DELETE</tt> statement on the record‘s primary key, and no callbacks are executed. </p> <p> To enforce the object‘s <tt>before_destroy</tt> and <tt>after_destroy</tt> callbacks, <a href="Observer.html">Observer</a> methods, or any <tt>:dependent</tt> association options, use <tt><a href="Base.html#M000467">destroy</a></tt>. </p> </div> </div> <div id="method-M000527" class="method-detail"> <a name="M000527"></a> <div class="method-heading"> <a href="Base.src/M000527.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000527.html');return false;"> <span class="method-name">destroy</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can‘t be persisted). </p> </div> </div> <div id="method-M000551" class="method-detail"> <a name="M000551"></a> <div class="method-heading"> <a href="Base.src/M000551.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000551.html');return false;"> <span class="method-name">eql?</span><span class="method-args">(comparison_object)</span> </a> </div> <div class="method-description"> <p> Delegates to == </p> </div> </div> <div id="method-M000553" class="method-detail"> <a name="M000553"></a> <div class="method-heading"> <a href="Base.src/M000553.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000553.html');return false;"> <span class="method-name">freeze</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Freeze the <a href="Base.html#M000543">attributes</a> <a href="Base.html#M000552">hash</a> such that associations are still accessible, even on destroyed records. </p> </div> </div> <div id="method-M000554" class="method-detail"> <a name="M000554"></a> <div class="method-heading"> <a href="Base.src/M000554.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000554.html');return false;"> <span class="method-name">frozen?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns <tt>true</tt> if the <a href="Base.html#M000543">attributes</a> <a href="Base.html#M000552">hash</a> has been frozen. </p> </div> </div> <div id="method-M000547" class="method-detail"> <a name="M000547"></a> <div class="method-heading"> <a href="Base.src/M000547.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000547.html');return false;"> <span class="method-name">has_attribute?</span><span class="method-args">(attr_name)</span> </a> </div> <div class="method-description"> <p> Returns true if the given attribute is in the <a href="Base.html#M000543">attributes</a> <a href="Base.html#M000552">hash</a> </p> </div> </div> <div id="method-M000552" class="method-detail"> <a name="M000552"></a> <div class="method-heading"> <a href="Base.src/M000552.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000552.html');return false;"> <span class="method-name">hash</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Delegates to <a href="Base.html#M000519">id</a> in order to allow two records of the same type and <a href="Base.html#M000519">id</a> to work with something like: </p> <pre> [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ] </pre> </div> </div> <div id="method-M000519" class="method-detail"> <a name="M000519"></a> <div class="method-heading"> <a href="Base.src/M000519.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000519.html');return false;"> <span class="method-name">id</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> A model instance‘s primary key is always available as model.id whether you name it the default ‘<a href="Base.html#M000519">id</a>’ or set it to something else. </p> </div> </div> <div id="method-M000522" class="method-detail"> <a name="M000522"></a> <div class="method-heading"> <a href="Base.src/M000522.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000522.html');return false;"> <span class="method-name">id=</span><span class="method-args">(value)</span> </a> </div> <div class="method-description"> <p> Sets the primary ID. </p> </div> </div> <div id="method-M000533" class="method-detail"> <a name="M000533"></a> <div class="method-heading"> <a href="Base.src/M000533.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000533.html');return false;"> <span class="method-name">increment</span><span class="method-args">(attribute, by = 1)</span> </a> </div> <div class="method-description"> <p> Initializes <tt>attribute</tt> to zero if <tt>nil</tt> and adds the value passed as <tt>by</tt> (default is 1). The <a href="Base.html#M000533">increment</a> is performed directly on the underlying attribute, no setter is invoked. Only makes sense for number-based <a href="Base.html#M000543">attributes</a>. Returns <tt>self</tt>. </p> </div> </div> <div id="method-M000534" class="method-detail"> <a name="M000534"></a> <div class="method-heading"> <a href="Base.src/M000534.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000534.html');return false;"> <span class="method-name">increment!</span><span class="method-args">(attribute, by = 1)</span> </a> </div> <div class="method-description"> <p> Wrapper around <tt><a href="Base.html#M000533">increment</a></tt> that saves the record. This method differs from its non-bang version in that it passes through the attribute setter. Saving is not subjected to validation checks. Returns <tt>true</tt> if the record could be saved. </p> </div> </div> <div id="method-M000557" class="method-detail"> <a name="M000557"></a> <div class="method-heading"> <a href="Base.src/M000557.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000557.html');return false;"> <span class="method-name">inspect</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns the contents of the record as a nicely formatted string. </p> </div> </div> <div id="method-M000523" class="method-detail"> <a name="M000523"></a> <div class="method-heading"> <a href="Base.src/M000523.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000523.html');return false;"> <span class="method-name">new_record?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns true if this object hasn‘t been saved yet — that is, a record for the object doesn‘t exist yet; otherwise, returns false. </p> </div> </div> <div id="method-M000556" class="method-detail"> <a name="M000556"></a> <div class="method-heading"> <a href="Base.src/M000556.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000556.html');return false;"> <span class="method-name">readonly!</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Marks this record as read only. </p> </div> </div> <div id="method-M000555" class="method-detail"> <a name="M000555"></a> <div class="method-heading"> <a href="Base.src/M000555.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000555.html');return false;"> <span class="method-name">readonly?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns <tt>true</tt> if the record is read only. Records loaded through joins with piggy-back <a href="Base.html#M000543">attributes</a> will be marked as read only since they cannot be saved. </p> </div> </div> <div id="method-M000539" class="method-detail"> <a name="M000539"></a> <div class="method-heading"> <a href="Base.src/M000539.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000539.html');return false;"> <span class="method-name">reload</span><span class="method-args">(options = nil)</span> </a> </div> <div class="method-description"> <p> Reloads the <a href="Base.html#M000543">attributes</a> of this object from the database. The optional options argument is passed to <a href="Base.html#M000458">find</a> when reloading so you may do e.g. record.reload(:lock => true) to <a href="Base.html#M000539">reload</a> the same record with an exclusive row lock. </p> </div> </div> <div id="method-M000524" class="method-detail"> <a name="M000524"></a> <div class="method-heading"> <a href="Base.src/M000524.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000524.html');return false;"> <span class="method-name">save(perform_validation = true)<br /> </span> </a> </div> <div class="method-description"> <p> Saves the model. </p> <p> If the model is <a href="Base.html#M000518">new</a> a record gets created in the database, otherwise the existing record gets updated. </p> <p> If <tt>perform_validation</tt> is true validations run. If any of them fail the action is cancelled and <tt><a href="Base.html#M000524">save</a></tt> returns <tt>false</tt>. If the flag is false validations are bypassed altogether. See <a href="Validations.html">ActiveRecord::Validations</a> for more information. </p> <p> There‘s a series of callbacks associated with <tt><a href="Base.html#M000524">save</a></tt>. If any of the <tt>before_*</tt> callbacks return <tt>false</tt> the action is cancelled and <tt><a href="Base.html#M000524">save</a></tt> returns <tt>false</tt>. See <a href="Callbacks.html">ActiveRecord::Callbacks</a> for further details. </p> </div> </div> <div id="method-M000525" class="method-detail"> <a name="M000525"></a> <div class="method-heading"> <a href="Base.src/M000525.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000525.html');return false;"> <span class="method-name">save!</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Saves the model. </p> <p> If the model is <a href="Base.html#M000518">new</a> a record gets created in the database, otherwise the existing record gets updated. </p> <p> With <tt><a href="Base.html#M000525">save!</a></tt> validations always run. If any of them fail <a href="RecordInvalid.html">ActiveRecord::RecordInvalid</a> gets raised. See <a href="Validations.html">ActiveRecord::Validations</a> for more information. </p> <p> There‘s a series of callbacks associated with <tt><a href="Base.html#M000525">save!</a></tt>. If any of the <tt>before_*</tt> callbacks return <tt>false</tt> the action is cancelled and <tt><a href="Base.html#M000525">save!</a></tt> raises <a href="RecordNotSaved.html">ActiveRecord::RecordNotSaved</a>. See <a href="Callbacks.html">ActiveRecord::Callbacks</a> for further details. </p> </div> </div> <div id="method-M000520" class="method-detail"> <a name="M000520"></a> <div class="method-heading"> <a href="Base.src/M000520.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000520.html');return false;"> <span class="method-name">to_param</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns a String, which Action Pack uses for constructing an URL to this object. The default implementation returns this record‘s <a href="Base.html#M000519">id</a> as a String, or nil if this record‘s unsaved. </p> <p> For example, suppose that you have a User model, and that you have a <tt>map.resources :users</tt> route. Normally, <tt>user_path</tt> will construct a path with the user object‘s ‘<a href="Base.html#M000519">id</a>’ in it: </p> <pre> user = User.find_by_name('Phusion') user_path(user) # => "/users/1" </pre> <p> You can override <tt><a href="Base.html#M000520">to_param</a></tt> in your model to make <tt>user_path</tt> construct a path using the user‘s name instead of the user‘s <a href="Base.html#M000519">id</a>: </p> <pre> class User < ActiveRecord::Base def to_param # overridden name end end user = User.find_by_name('Phusion') user_path(user) # => "/users/Phusion" </pre> </div> </div> <div id="method-M000537" class="method-detail"> <a name="M000537"></a> <div class="method-heading"> <a href="Base.src/M000537.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000537.html');return false;"> <span class="method-name">toggle</span><span class="method-args">(attribute)</span> </a> </div> <div class="method-description"> <p> Assigns to <tt>attribute</tt> the boolean opposite of <tt>attribute?</tt>. So if the predicate returns <tt>true</tt> the attribute will become <tt>false</tt>. This method toggles directly the underlying value without calling any setter. Returns <tt>self</tt>. </p> </div> </div> <div id="method-M000538" class="method-detail"> <a name="M000538"></a> <div class="method-heading"> <a href="Base.src/M000538.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000538.html');return false;"> <span class="method-name">toggle!</span><span class="method-args">(attribute)</span> </a> </div> <div class="method-description"> <p> Wrapper around <tt><a href="Base.html#M000537">toggle</a></tt> that saves the record. This method differs from its non-bang version in that it passes through the attribute setter. Saving is not subjected to validation checks. Returns <tt>true</tt> if the record could be saved. </p> </div> </div> <div id="method-M000530" class="method-detail"> <a name="M000530"></a> <div class="method-heading"> <a href="Base.src/M000530.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000530.html');return false;"> <span class="method-name">update_attribute</span><span class="method-args">(name, value)</span> </a> </div> <div class="method-description"> <p> Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular <tt><a href="Base.html#M000530">update_attribute</a></tt> method in <a href="Base.html">Base</a> is replaced with this when the validations module is mixed in, which it is by default. </p> </div> </div> <div id="method-M000531" class="method-detail"> <a name="M000531"></a> <div class="method-heading"> <a href="Base.src/M000531.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000531.html');return false;"> <span class="method-name">update_attributes</span><span class="method-args">(attributes)</span> </a> </div> <div class="method-description"> <p> Updates <a href="Base.html#M000461">all</a> the <a href="Base.html#M000543">attributes</a> from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned. </p> </div> </div> <div id="method-M000532" class="method-detail"> <a name="M000532"></a> <div class="method-heading"> <a href="Base.src/M000532.html" target="Code" class="method-signature" onclick="popupCode('Base.src/M000532.html');return false;"> <span class="method-name">update_attributes!</span><span class="method-args">(attributes)</span> </a> </div> <div class="method-description"> <p> Updates an object just like <a href="Base.html#M000531">Base.update_attributes</a> but calls <a href="Base.html#M000525">save!</a> instead of <a href="Base.html#M000524">save</a> so an exception is raised if the record is invalid. </p> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>