从下面的代码中,当我打电话时conn.params["geo"],我收到以下错误:
test/plugs/geoip_test.exs:4
** (UndefinedFunctionError) function Plug.Conn.Unfetched.fetch/2 is undefined (Plug.Conn.Unfetched does not implement the Access behaviour)
stacktrace:
(plug) Plug.Conn.Unfetched.fetch(%{:__struct__ => Plug.Conn.Unfetched, :aspect => :params, "geo" => "Mountain View, US", "ip" => "8.8.8.8"}, "geo")
Run Code Online (Sandbox Code Playgroud)
...
defmodule AgilePulse.Plugs.GeoIPTest do
use AgilePulse.ConnCase
test "returns Mountain View for 8.8.8.8" do
conn = build_conn
params = Map.put(conn.params, "ip", "8.8.8.8")
conn = Map.put(conn, :params, params) |> AgilePulse.Plugs.GeoIP.call(%{})
assert conn.params["geo"] == "Mountain View, US"
end
end
defmodule AgilePulse.Plugs.GeoIP do
import Plug.Conn
def init(opts), do: opts
def …Run Code Online (Sandbox Code Playgroud) <hyperbole>根据您的真实情况,无论谁回答这个问题,都可以为解决世界上最具挑战性的SQL查询提供信誉.</hyperbole>
使用3个表:用户,徽章,奖励.
关系:用户有很多奖项; 奖项属于用户; 徽章有很多奖项; 奖项属于徽章.因此,badge_id和user_id是奖励表中的外键.
这里的业务逻辑是每次用户赢得徽章时,他/她都会将其作为奖励收到.用户可以多次获得相同的徽章.每个徽章都分配了一个指定的点值(point_value是徽章表中的一个字段).例如,BadgeA可以值500点,BadgeB 1000点,等等.举个例子,假设UserX赢了BadgeA 10次,BadgeB赢了5次.BadgeA值500点,BadgeB值1000点,UserX累计累积10,000点((10 x 500)+(5 x 1000)).
这里的最终游戏是返回累积最多徽章积分的前50名用户的列表.
你可以做到吗?
我有一系列称为领导者的结构.对于上下文信息,struct类看起来像这样:
class Leader < Struct.new(:rank, :user); end
Run Code Online (Sandbox Code Playgroud)
两个问题:
有没有办法干掉这个CSS?唯一不同的是颜色?
div.base-text-gold {
position: absolute; bottom: 9px; color: #FED577; font-size: 10px;
font-weight: bolder; text-align: center; width: 61px; text-transform: uppercase;
}
div.base-text-grey {
position: absolute; bottom: 9px; color: #D1D2D4; font-size: 10px;
font-weight: bolder; text-align: center; width: 61px; text-transform: uppercase;
}
Run Code Online (Sandbox Code Playgroud) 我有两节课:
class Activity < ActiveRecord::Base
belongs_to :activity_type
def belongs_to_cat_a?
self.activity_category == ActivityCategory.category_a
end
def belongs_to_cat_b?
self.activity_category == ActivityCategory.category_b
end
end
class ActivityCategory < ActiveRecord::Base
has_many :activities
def self.cat_a
ActivityCategory.find_by_name("CatA")
end
def self.cat_b
ActivityCategory.find_by_name("CatB")
end
end
Run Code Online (Sandbox Code Playgroud)
使用元编程,我将ActivityCategory更改为以下内容:
class ActivityCategory < ActiveRecord::Base
has_many :activities
CATEGORIES = ['CatA', 'CatB']
class << self
CATEGORIES.each do |c|
define_method "#{c.underscore.downcase}" do # for ex: cat_a
find_by_name(c)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
好.现在想象一下在Activity类中我有大约12种方法来检查它属于哪个类别.
看起来像是使用MP干掉一点的完美候选人.
我怎么能这样做?
似乎应该有一个很好的方式通过MP来干这个:
class Dashboard
def self.num_registrations_past_day
return User.recent_registrations(24.hours.ago).count
end
def self.num_registrations_past_three_days
return User.recent_registrations(3.days.ago).count
end
def self.num_registrations_past_seven_days
return User.recent_registrations(7.days.ago).count
end
def self.num_registrations_past_month
return User.recent_registrations(30.days.ago).count
end
def self.avg_registrations_past_three_days
return (self.num_registrations_past_three_days / 3.to_f)
end
def self.avg_registrations_past_seven_days
return (self.num_registrations_past_seven_days / 7.to_f)
end
def self.avg_registrations_past_month
return (self.num_registrations_past_month / 30.to_f)
end
def self.total_registered_users
return User.count
end
def self.total_activated_users
return User.total_activated
end
end
Run Code Online (Sandbox Code Playgroud) 假设我有一个名为Article的模型:
class Article < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
然后我有一个用于向文章对象(装饰器)添加行为的类:
class ArticleDecorator
def format_title
end
end
Run Code Online (Sandbox Code Playgroud)
如果我想扩展一个文章对象的行为,我可以使ArticleDecorator成为一个模块,然后调用article.extend(ArticleDecorator),但我更喜欢这样的东西:
article = ArticleDecorator.decorate(Article.top_articles.first) # for single object
Run Code Online (Sandbox Code Playgroud)
要么
articles = ArticleDecorator.decorate(Article.all) # for collection of objects
Run Code Online (Sandbox Code Playgroud)
我将如何实现这种装饰方法?
我正在尝试编写一个RegEx来验证浮点数.这是我到目前为止所管理的内容:
/^[-+]?[1-9]\d{0,2}(\.\d{1,1})?/
Run Code Online (Sandbox Code Playgroud)
该号码有效,如果:
所以这些数字是有效的,例如:
这些数字无效,例如:
我有射击游戏,用户在一周内相互竞争,积累最多积分.我想写一个聚合来自镜头表的统计数据的查询.这里的表格和关系是:
在镜头表中,我有以下字段可供使用:
对于每个用户,我想返回一个带有以下聚合统计数据的结果集:
我现在已经在这个查询上工作了几个小时,但没有取得多大进展.统计功能让我兴奋不已.一位朋友建议我尝试在名为shot_records的新虚拟表中返回结果.