Search code examples
ruby-on-railsrubyframeworksscaffoldingscaffold

List all methods related to a model which scaffold creates


Is there a way to list all methods which scaffold creates regarding Ruby on Rails?

Example:

rails generate scaffold user name:string admin:references

Now, within the controller, the following declaration is possible:

@user = User.new 
@user.admin=current_admin

Now, I want to check where the admin() method comes from - or where it is placed respectively.

I tried to figure out that topic with the help of the following book: Michael Hartl - Ruby on Rails Tutorial Learn Web Development with Rails


Solution

  • You can use this snippet to find where a specific method is defined (User#admin=) in your case:

    User.instance_method(:admin=).source_location
    

    And it will likely return something like this, depending on your Ruby and Ruby on Rails version:

    ["[...]/3.2.0/gems/activerecord-7.0.6/lib/active_record/associations/builder/association.rb", 111]
    

    Which will tell you that it is a method auto-generated by Ruby on Rails for an association.

    The next step would then be to look into the source code of User for a defined association like belongs_to :admin.