newbie question basic syntax to call field (active record) in rails
I am learning some basic in rails, and found a difficulty in understanding a code.
I have this method
def mymethod
cup = Cup.find_by_id(current_cup.id)
result, log = cup.parse(mug.bought_date)
end
There are two questions that I have for the code above
The cup is taking the value based on id with the parameter of current cup id. But I got lost with result, log because I can"t find "result, log" function for rails in google (or maybe I miss it). Or maybe it is a defined function ? the second question is that, in the mysql table, I don"t find any columns of "parse". Howcome the cup.parse is called 开发者_如何学Goif there is no "parse" columns in cup row? Is it, again, a defined function by the coder, if yes, how can I make those kind of function?
Sorry for this super basic question, but I tried to read and can"t find the proper explanation. But I believe that learning directly from the experts can help me to the correct path.
Thank you so much
The Cup
model must have a method called parse
. Look in app/models/cup.rb
and you should see a def parse
in there. Rails models expose database columns as methods, yes, but you can also add methods that dont correspond to the database at all. This is because a model class is a just a ruby class like any other ruby class.
The second part confusing you is called destructuring assignment. cup.parse
returns an array with 2 elements. You can use that syntax to pull the values out of the returned array and assigned to local variables. For example:
var1, var2, var3 = [:a, :b, :c]
puts var1 #=> a
puts var2 #=> b
puts var3 #=> c
This pattern allows a method to return mutliple values, what are then easily assigned to local variables.
精彩评论