某些插件 的一点小特性

对于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 文件
生成所增加的迁移文件
生成‘真正自己的’文件

转载于:https://my.oschina.net/kelby/blog/193129

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值