我是铁杆上的红宝石新手.并且在玩测试
两者之间有区别吗?
before(:each) do
#some test code
end
Run Code Online (Sandbox Code Playgroud)
和
before do
#some test code
end
Run Code Online (Sandbox Code Playgroud) 我已经建模,用户参加了活动.
class User
has_many :attendances
has_many :events, through: :attendances
class Event
has_many :attendances
scope :is_attending, -> { joins(:attendances).where(attendances:{attend_status: Attendance.attend_statuses[:attending] })}
class Attendance
belongs_to :event
belongs_to :user
enum attend_status: { attending: 0, not_attending: 1}
Run Code Online (Sandbox Code Playgroud)
我的问题是关于范围查询和最佳实践.
我已将大部分范围查询放在Event上.
我想获得READ_status = 0的特定用户的所有事件
user = User.find(...)
user.events.is_attending
Run Code Online (Sandbox Code Playgroud)
从逻辑上讲,我认为,这是最好的,也是最有意义的
然而,这将给我一个双INNER JOIN
SELECT "events".* FROM "events"
INNER JOIN "attendances" "attendances_events" ON "attendances_events"."event_id" = "events"."id"
INNER JOIN "attendances" ON "events"."id" = "attendances"."event_id"
WHERE "attendances"."user_id" = $1 AND "attendances"."attend_status" = 0
Run Code Online (Sandbox Code Playgroud)
显然,这会产生重复,这不是我想要的.
所以我知道我可以做的选择
1)使用MERGE
Event
scope :for_user, -> (user){ …Run Code Online (Sandbox Code Playgroud) 文档暗示inverse_of将适用于除多态关联之外的所有内容. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
然而,似乎inverse_of仍然不适用于has_many:through
例如,我在以下示例中尝试使用inverse_of的每个组合都不起作用
class Event < ActiveRecord::Base
has_many :attendances
has_many :users, through: :attendance
class User < ActiveRecord::Base
has_many :attendances
has_many :events, through: :attendances
class Attendance < ActiveRecord::Base
belongs_to :event
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
任何想法都应该有效吗?如果是这样,我将如何设置此示例中的inverse_of?
例如,我尝试过(也试过has_many:通过)
class Event < ActiveRecord::Base
has_many :attendances , :inverse_of => :event
has_many :users, through: :attendance
end
class User < ActiveRecord::Base
has_many :attendances , :inverse_of => :user
has_many :events, through: :attendances
end
class Attendance < ActiveRecord::Base
belongs_to :event, :inverse_of => :attendances
belongs_to :user, :inverse_of => :attendances
end …Run Code Online (Sandbox Code Playgroud) 我正在尝试为特定区域设置添加特定月份名称.我遇到的问题是挪威语bokmal(nb)的语言环境SimpleDateFormat返回的月份名称是英语而不是norweigan.但是,似乎locale(no)工作正常
例如,这段代码会产生一月,二月等.
String pattern = "MMMM";
DateFormat monthFormat = new SimpleDateFormat(pattern, new Locale("nb"));
Calendar cal = Calendar.getInstance();
for (int i = 0; i < 12; i++) {
cal.set(Calendar.MONTH, i);
System.out.println(monthFormat.format(cal.getTime()));
}
Run Code Online (Sandbox Code Playgroud)
vs此代码导致Januar,Februar等
String pattern = "MMMM";
DateFormat monthFormat = new SimpleDateFormat(pattern, new Locale("no"));
Calendar cal = Calendar.getInstance();
for (int i = 0; i < 12; i++) {
cal.set(Calendar.MONTH, i);
System.out.println(monthFormat.format(cal.getTime()));
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用特定的DateFormatSymbols配置SimpleDateFormat但是这无助于保持我的代码对于任何未来的本地人都是通用的.我想知道是否有人知道如何修改受支持的java语言环境的默认月份值?我想我会添加一个资源文件,但无法弄清楚如何.
我正在构建第三方小部件
我们在客户端页面上删除脚本并加载一些内容.
我面临的问题是如何保护我的小部件.作为一个第三方小部件我知道没有100%的方法来保护它.但试图找出一个"足够好"的方法.
我想让非客户很难将我们的脚本从他们的竞争对手网站上删除并在他们的网站上使用它.
我看到的解决方案是拉验证请求域(我知道可能是欺骗,不确定我是否可以防范这个?)
我看了一下像olark和olapic这样的其他小部件,它们在脚本中使用每个客户端的唯一id,但看不出它有多大帮助.
保护第三方窗口小部件的最佳做法是什么?
我试图在java中解决"找到你附近的兴趣点"的问题.即我有一些lat,很多积分.我想找到800米以内的那些.假设我知道我当前的lat和log.
我的问题是这个应用程序必须独立.所以我不能依赖空间数据库查询来查找结果,例如使用mysql我发现这很有用:http: //xebee.xebia.in/2010/10/28/working-with-geolocations/
目前我找不到支持spatital查询的内存数据库.我打算使用像http://jsi.sourceforge.net/这样的R-Tree.
但我知道这不会是正确的,因为它使用的是矩形而不是迂回搜索.
有什么建议吗?
有一个奇怪的问题,
我正在测试webfonts
这是我的CSS
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans'), local('OpenSans'), url(/assets/opensans.woff) format('woff');
}
Run Code Online (Sandbox Code Playgroud)
我在整个应用程序中使用该字体,并且在一个页面上没有任何问题.我在这个页面上有相当数量的JS,JS定位元素并设置正确div的高度.
在一些请求上webfont没有加载(根本)
例如

我可以在Windows 7上的chrome上重现这个.但有时候只是.
那些盒子应该是文本,你可以看到的文字是我没有使用@ font-face的地方.
我有javascript事件绑定到窗口调整大小,改变右div的宽度高度.有趣的是,当我调整窗口大小时,字体似乎加载.
我在控制台或chrome上的网络选项卡中看不到任何错误.
它失去了可能性.
我有以下型号
class Event < ActiveRecord::Base
has_many :attendances
class Attendance < ActiveRecord::Base
belongs_to :user
class Student < User
has_one :student_detail
class StudentDetail < ActiveRecord::Base
belongs_to :school
class Staff < User
has_one :staff_detail
class StaffDetail < ActiveRecord::Base
Run Code Online (Sandbox Code Playgroud)
StudentDetail和StaffDetails有额外的信息,我试图避免在一个STI用户表中使用它,因为必须使用类似于每个表模式的具体类的东西
我可以很容易地做到这一点
Event.includes(:attendances => :user).where(...)
Run Code Online (Sandbox Code Playgroud)
但我想能够包括取决于用户类型,例如
Event.includes(attendances: {:user => :student_details })
Run Code Online (Sandbox Code Playgroud)
这将失败,因为一些用户是Staff对象.
我意识到rails不支持开箱即用,但任何人都有任何技巧可以让它工作
现在最好的解决方案是分散用户出席学生和员工即
class Attendance < ActiveRecord::Base
belongs_to :student, -> {includes(:staff_detail) }
belongs_to :staff, -> {includes(:student_detail) }
#belong_to :user
Run Code Online (Sandbox Code Playgroud)
这不是理想的.有人有任何提示吗?解决这个问题的方法.
我有一个相当简单的mongodb模型目前2个系列学生课程(嵌入主题)
class Student < User
include Mongoid::Document
has_many :courses
class Course
include Mongoid::Document
belongs_to :student
embeds_many :subjects
Run Code Online (Sandbox Code Playgroud)
我知道mongoid不支持直接查询has_many即
Student.courses
Run Code Online (Sandbox Code Playgroud)
因此,如果我想获得学生科目,这是唯一的方法
@student = Student.first
Course.find(@student.courses).subjects
Run Code Online (Sandbox Code Playgroud)
这目前有3个查询
MOPED: 127.0.0.1:27017 QUERY database=test collection=users selector= {"$query"=>{"_type"=>{"$in"=>["Student"]}}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (1.1775ms)
MOPED: 127.0.0.1:27017 QUERY database=test collection=courses selector={"student_id"=>"51f09457b5b605db25000002"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.8862ms)
MOPED: 127.0.0.1:27017 QUERY database=test collection=courses selector={"_id"=>"51f09457b5b605db25000003"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.8380ms)
Run Code Online (Sandbox Code Playgroud)
有更有效的方法吗?我知道我可以在课程中嵌入课程,但我也会直接查询课程集.
对于我所有的js:真正的特征规格,例如
scenario "test something", js: true do
end
Run Code Online (Sandbox Code Playgroud)
我想说运行一些任意代码,例如
visit signin_path
Run Code Online (Sandbox Code Playgroud)
我知道我可以在spec_helper.rb中做
config.before(:each) do
visit signin_path
end
Run Code Online (Sandbox Code Playgroud)
但是我只想用js:true运行这个规格
有没有办法做到这一点?
想知道是否有人看到过这个问题.我正在使用sucker_punch gem版本1.1在Passenger 3上运行Rails 3.2
我有一个长期运行的sucker_punch工作(大约需要10个小时)这是一个整夜的批次.我在Phusion Passenger上运行(我认为3个工作线程)
status from passenger-status
----------- General information -----------
max = 3
count = 0
active = 0
inactive = 0
Waiting on global queue: 0
Run Code Online (Sandbox Code Playgroud)
我的sucker_punch作业执行异步,作为其执行其他异步较小的sucker_punch作业的作业的一部分(每个约需30秒)
我无法准确地确定发生了什么,但"有时"我的长期工作刚刚死亡或似乎停止了.我确实在整个sucker_punch作业中添加了一些调试代码
begin
rescue Exception => e
logger.error(e)
raise e
end
Run Code Online (Sandbox Code Playgroud)
但是没有看到异常,那么假设我的长时间运行sucker_punch正在停止而不是被杀死?还是潜在的某种僵局?
这个有趣的部分.有时我的长期工作运作良好,有时却没有.
activerecord ×2
java ×2
javascript ×2
rspec ×2
capybara ×1
css3 ×1
geolocation ×1
mongodb ×1
mongoid ×1
passenger ×1
sti ×1
sucker-punch ×1
widget ×1