对于DB的操作无非就是 ‘增、删、查、改’,可对应着HTTP的 POST, DELETE, GET, PUT,。
所以在理解某些插件的 model “方法”时,同样可以像在controller里一样,分类为 GET, 非GET 操作。
上面所说的目的:有的插件提供的方法很多,死记硬背会显得很傻,先分好类就容易多了。
# Votes up the question by the user.
# If the user already voted the question up then an AlreadyVotedError is raised.
# If the same user already voted the question down then the vote is changed to an up vote.
user.up_vote(question)
# Votes the question up, but without raising an AlreadyVotedError when the user
# already voted the question up (it just ignores the vote).
user.up_vote!(question)
# Votes down the question by the user.
# If the user already voted the question down then an AlreadyVotedError is raised.
# If the same user already voted the question up then the vote is changed to an down vote.
user.down_vote(question)
# Votes the question down, but without raising an AlreadyVotedError when the user
# already voted the question down (it just ignores the vote).
user.down_vote!(question)
# Clears a already done vote by an user.
# If the user didn't vote for the question then a NotVotedError is raised.
user.unvote(question)
# Does not raise a NotVotedError if the user didn't vote for the question
# (it just ignores the unvote).
user.unvote!(question)
# The number of up votes for this question.
question.up_votes
# The number of down votes for this question.
question.down_votes
# The number of up votes the user did.
user.up_votes
# The number of down votes the user did.
user.down_votes
# up votes - down votes (may also be negative if there are more down votes than up votes)
question.votes
# Returns true if the question was voted by the user
user.voted?(question)
# Returns true if the question was up voted by the user, false otherwise
user.up_voted?(question)
# Returns true if the question was down voted by the user, false otherwise
user.down_voted?(question)
# Access votings through voter
voting = user.votings.first
voting.up_vote? # true if up vote, false if down vote
# Access votings through voteable
voting = question.votings.first
voting.up_vote? # true if up vote, false if down vote
对于DB的操作无非就是 ‘增、删、查、改’,可对应着HTTP的 POST, DELETE, GET, PUT,。 所以在理解某些插件的 model “方法”时,同样可以理解为 GET, 非GET 操作。 上面所说的目的:有的插件提供的方法很多,死记硬背会显得很傻,先分好类就容易多了。
rails插件初始化
rails 中的一些插件,其初始化时通常做以下几事:
在原有的文件‘增加’自己的代码。
生成 initializer 文件
生成 locale 文件
生成所增加的迁移文件
生成‘真正自己的’文件