ruby - Rails: How To Track Down A Method Called On An Array That Has No Such Method? -
i'm trying figure out in rails application. have class site has themes (of class theme) , when site uploads theme can install specific site. i'm tracking down code , see calls method 'build' this:
@theme = @current_site.themes.build(params[:theme])
the themes method returns array of theme objects associated site. in rails console tried (result included):
>> site.themes.method(:build).__file__ nameerror: undefined method `build' class `array'
and tried this:
>> site.themes.respond_to? :build => true
so question twofold, how can find out 'build' defined can understand how works?, , can explain me how method 'build' works in terms of being able called @ end of array object has no such method? thanks!
given site
model has_many :themes
, 1 of association methods automatically gets added association.
if want more reading material, check out association basics guide (for example, section on has_many
associations), lists of methods added along parameters take.
as how works, .themes
object associationproxy
(source here), array
target, not array
itself. .class
returns 'array'
because proxy not respond .class
method, , instead forwards target (the array
) via method_missing
. this answer gives more detailed explanation.
Comments
Post a Comment