Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question is not clear. By "models" you mean database-backed models that use ActiveRecord, right? </p> <p>Usually validation is not a "file" but is a series of statements within the model's file. Same for relationship declarations.</p> <p>You can split a model's contents amongst any number of files. The technique varies depending on whether you want the other files to contain instance methods or class methods. </p> <p>Easiest is to have instance methods in other files. Eg</p> <pre><code># file foo.rb class Foo &lt; ActiveRecord::Base include FooMethods # --- callbacks --- # before_create :record_signup # "before_create" is a "callback". # See http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html # note that the method could also be from a different class altogether: before_save EncryptionWrapper.new # See section "Types of callbacks" in the api url referred to above # --- relationships --- # belongs_to :foobar has_many :bars # --- Class Methods --- # def Foo.a_method_name(id) ... end end ~~~~~~~~~~~~~~~~~~~~~~~~~~~ # file foo_methods.rb module FooMethods def method1 ... end def method2 ... end private def record_signup # define the callback method self.signed_up_on = Date.today end end </code></pre> <p>Offhand, I don't how to put the callback override </p> <blockquote> <p>before_create</p> </blockquote> <p>in a different file than the model's main class file. Wouldn't be hard to figure out. But no matter how easy it would be to put in a different file, I would not recommend it from the code clarity point of view.</p>
 

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