I have two models,
User
Membership
The two have the following relationship with one another
user has_many :memberships
I’ve been trying to figure out where the build method resides, and how do i get it in a list of methods for the instance. Here is the output of the debugger that shows my delima
(rdb:63) @user.memberships.respond_to?"build"
true
While the following is returning false, shouldnt it return true??
(rdb:63) @user.memberships.instance_methods.include?"build"
false
One point is that
instance_methodstakes an optional boolean parameter, indicating whether you want to see methods of the instances ancestors. In your case I think you wantinstance_methods(true).However, it appears that “build” is an autogenerated method, according to the documentation. Typically the autogenerated methods in ActiveRecord are implemented by overriding
method_missingand handling calls to “methods” that don’t actually exist.responds_tois also overridden so that the class will indicate that it responds to the correct calls. However, since those “methods” aren’t actually defined, they won’t show up in theinstance_methodslist.Since the list of commands that a class can
respond_tousingmethod_missingis essentially infinite, I’m pretty sure there’s no way to get the list. For example, an ActiveRecord model that has attributes a,b,c, and d will automatically respond to calls such asfind_by_a_and_bandfind_by_a_b_and_candfind_by_b_and_dand so forth, ad infinitum. There’s no way to get a list of all of those possibilities.