I’m a new Ruby/Rails guy. Here’s one question puzzling me:
Can we find the exact module lists mixin-ed for a class in Rails from the API doc? For example, if we have an instance of one subclass of ActiveRecord::Base, we can use validates method in this class such as following:
class Product < ActiveRecord::Base
has_many :line_items
validates :title, :description, :image_url, :presence => true
end
from rails api doc we can find that validates belongs to ActiveModel::Validations::ClassMethods, so ActiveRecore::Base must have ActiveModel::Validations::ClassMethods mixin, but I didn’t find anything relating to this in the api reference. Can anyone tell me if I can find this info from api doc?
Thanks for all of your help in advance. I really hope my question doesn’t sound too silly:)
I don’t see anything like that in any of the Rails documentation I’ve seen:
And with monkey patching, there probably isn’t any way to know until Rails is all cranked up and you have an actual ActiveRecord::Base in your hands; for example, the PostgreSQL, MySQL, and SQLite adapters re-open ActiveRecord::Base and they might include extra modules along the way; other gems you’re using and even your own code may do similar things.
If you just want to know the standard set of mixins, use the source (Luke!) or do as maniek suggests and ask ActiveRecord::Base in the Rails console.
In general, the Rails API documentation isn’t that useful unless you already know what you’re looking for and even then, digging into the source is often necessary.
Your best bet is to check the guides for what you need and read the source if that doesn’t work.
Short Answer: You can’t get your list from the documentation because the modules cannot be known until run time. Read the guides and the source if you need to know something.