the following method is intended to extract an object of the class Categoryminor based on selection criteria in a controller action
def get_categoryminor(categorymajor_name, categoryminor_name)
cmajor = Categorymajor.find_by(name: categorymajor_name)
cminor = Categoryminor.find_by(categorymajor_id: cmajor.id, name: categoryminor_name)
end
In the console:
get_categoryminor('generic_grouping', 'food')
#<Categorymajor id: 30, name: "generic_grouping", [...]
#<Categoryminor id: 205, categorymajor_id: 30, name: "food" [...]
> nil
however, while the method runs and queries the database as expected, the method returns nil.
how could a controller action instruction @categoryminor_food = get_categoryminor('generic_grouping', 'food')
then use the value generated from the database query?
3
You are missing return
values from the controller method.
def get_categoryminor(categorymajor_name, categoryminor_name)
cmajor = Categorymajor.find_by(name: categorymajor_name)
cminor = Categoryminor.find_by(categorymajor_id: cmajor.id, name: categoryminor_name)
return cmajor, cminor
end
Assign returned variables to new objects:
@major, @minor = get_categoryminor('generic_grouping', 'food')
1