Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how it works</p> <ol> <li><p>On first access to model, columns from corresponding database table are retrieved and stored inside model data. This information can be retrieved through ::<a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002294" rel="noreferrer">columns</a> class method.</p></li> <li><p>When you access some model's attribute, Ruby doesn't find corresponding method in class and launches #method_missing method. That method inspects model's ::<a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002294" rel="noreferrer">columns</a> to check if corresponding column exists. If so, it creates an accessors for that column so that next time you access that model's attribute, an accessor method will be called directly, without need to call #method_missing (the later is slower).</p></li> </ol> <p>The accessors look like this:</p> <pre><code>def my_attribute read_attribute(:my_attribute) end def my_attribute=(value) write_attribute(:my_attribute, value) end </code></pre> <p>For #read_attribute and #write_attribute methods there is a shortcut: #<a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002345" rel="noreferrer">[]</a> and #<a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002346" rel="noreferrer">[]=</a>. If for some reason you will need to access underlying data directly (e.g. do some data conversion), you can write them short:</p> <pre><code>def my_attribute self[:my_attribute] end def my_attribute=(value) self[:my_attribute] = value end </code></pre> <p>Model has a special accessor -- #<a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002348" rel="noreferrer">attributes</a> -- which returns a "column_name => value" Hash.</p> <p>NOTE: the data for each column is stored in a special Hash instance inside your model instance, not in "@column_name" instance variables. When you define accessors with #attr_accessor, you block the usual way of defining attribute accessors via #method_missing. Your data is stored in instance variables instead of "<a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002348" rel="noreferrer">attributes</a>" hash, so it is not saved into database.</p> <p>If you want to add new attribute to your model, you actually need to add column to database table that correspond to that model and then reload the whole application.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload