小编Mar*_*als的帖子

如何检查两个其他值之间是否包含值?

我试图表达这样的条件:

if 33.75 < degree <= 56.25
  # some code
end
Run Code Online (Sandbox Code Playgroud)

但是Ruby给出了这个错误:

undefined method `<=' for true:TrueClass
Run Code Online (Sandbox Code Playgroud)

我猜测一种方法是这样的:

if 33.75 < degree and degree <= 56.25
  # code
end
Run Code Online (Sandbox Code Playgroud)

但是没有另一种更简单的方法吗?

ruby conditional-statements

22
推荐指数
3
解决办法
2万
查看次数

从数据库rails获取所有user.name

我猜测如何只检索数据库中所有记录的名称.简单的方法是这样的:

@user=User.all
@list=[]
@user.each do|u|
  @list.push(u.name)
end
Run Code Online (Sandbox Code Playgroud)

但我确定它是否必须是irb的oneline查询必须适合.有没有人有想法?

ruby ruby-on-rails

5
推荐指数
2
解决办法
3977
查看次数

TypeError:providers [name] [0]不是Angular 4.0.1 npm test函数

嗨,我是Angular2的新手,当我尝试使用onicial angular.io网站上的示例运行npm测试时,我收到此错误:

providers[name][0] is not a function
TypeError: providers[name][0] is not a function
    at get (/Users/marcpursals/repos/..../node_modules/di/lib/injector.js:48:43)
    at /Users/marcpursals/repos/...../node_modules/di/lib/injector.js:71:14
    at Array.map (native)
    at Injector.invoke (/Users/marcpursals/repos/...../node_modules/di/lib/injector.js:70:31)
    at Server.start (/Users/marcpursals/repos/....node_modules/karma/lib/server.js:103:18)
    at Promise (/Users/marcpursals/repos/..../node_modules/@angular/cli/tasks/test.js:35:25)
    at Class.run (/Users/marcpursals/repos/..../node_modules/@angular/cli/tasks/test.js:15:16)
    at Class.run (/Users/marcpursals/repos/....node_modules/@angular/cli/commands/test.js:99:25)
    at Class.<anonymous> (/Users/marcpursals/repos/...../node_modules/@angular/cli/ember-cli/lib/models/command.js:134:17)
    at process._tickCallback (internal/process/next_tick.js:103:7)
npm ERR! Test failed.  See above for more details.
Run Code Online (Sandbox Code Playgroud)

我正在使用angular 4.0.1,我的package.json是下一个:

"dependencies": {
    "@angular/animations": "^4.0.1",
    "@angular/common": "^4.0.1",
    "@angular/compiler": "^4.0.1",
    "@angular/compiler-cli": "^4.0.1",
    "@angular/core": "^4.0.1",
    "@angular/forms": "^4.0.1",
    "@angular/http": "^4.0.1",
    "@angular/material": "^2.0.0-beta.2",
    "@angular/platform-browser": "^4.0.1",
    "@angular/platform-browser-dynamic": "^4.0.1",
    "@angular/platform-server": "^4.0.1",
    "@angular/router": "^4.0.1",
    "@ng-bootstrap/ng-bootstrap": …
Run Code Online (Sandbox Code Playgroud)

testing angular

5
推荐指数
1
解决办法
366
查看次数

Faker:不知道如何构建任务'环境'

我有这个sample_data.rake文件:

namespace :db do
desc "Fill Patients with sample data"
task populate: enviroment do
  Patient.create!(name: ["Example", "User"],
                  email: "example@gmail.com"
                  password: "foobar"
                  password_confirmation: "foobar"
                  age: "26"
                  doctor_id: "3"
                  dao: "true"
                  active: "true")
  350.times do |n|
    name=Faker::Name.name
    email = "example-#{n+1}@gmail.com"
    password = "password"
    age = (25...45).sample
    doctor_id = [2,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22].sample
    dao = ["true", "false"].sample
    active = "true"
    Patient.create!(name: name,                     
                email: email,                   
                password: password,             
                password_confirmation: password,
                age: age,                       
                doctor_id: doctor_id            
                dao: dao,                       
                active: active)  
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

它放在lib/tasks上,当我运行rake db:populate我收到下一个错误.

rake aborted!
Don't know how …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

1
推荐指数
1
解决办法
2034
查看次数

Ruby双重迭代

我想知道是否有最性感的方式进行双重迭代,我的意思是,我有两次迭代在同一个关系上,如下所示:

bars = ActiveRecord::Relation [.....]
bars.each do |bar|
  do_something1(bar)
  do_something2(bar)
end
bars.each do |bar|
  do_something3(bar)
  do_something4(bar)
end
Run Code Online (Sandbox Code Playgroud)

显而易见的答案是:

bars.each do |bar|
  do_something1(bar)
  do_something2(bar)
  do_something3(bar)
  do_something4(bar)
end
Run Code Online (Sandbox Code Playgroud)

但实际上我真的需要先完成do_something1(bar)和do_something2(bar),然后在同一关系上执行do_something3(bar)和do_something4(bar).可能这是一个新手问题,但我尝试的任何东西都比这更难.有什么建议吗?

提前致谢.

更新:对不起,我忘了说我已经尝试了这个:

bars.each do |bar|
  do_something1(bar)
  do_something2(bar)
end.each do |bar|
  do_something3(bar)
  so_something4(bar)
end
Run Code Online (Sandbox Code Playgroud)

但是在代码理解方面,很容易理解第一个和改进它只是一个词,这是我没有使用它的方式.

ruby ruby-on-rails-4

1
推荐指数
1
解决办法
617
查看次数

数据库文件在哪里?轨道

我是Rails的新手,我刚刚完成了我正在开发的应用程序的一部分,我想知道数据库文件在哪里,我无法在rails.root目录中找到它.有没有人有答案?

提前致谢.

编辑:我正在搜索文件而不是配置文件,我正在使用PostgreSQL和Ubuntu

ruby postgresql ruby-on-rails

0
推荐指数
1
解决办法
4400
查看次数

自我方法(params)Rails

我是新手,我想知道是否可以用params定义模型方法.我的意思是.我有这种方法用球坐标计算距离

#in my model

#Haversin formula to calculate distance between spherical coordinates 
def self.distance(b)                                                
  rad_per_deg = Math::PI/180  # PI / 180                                                      
  rkm = 6371                  # Earth radius in kilometers                                    
  #rm = rkm * 1000            # Radius in meters                                              
  a=[]                                                                                        
  a.push(self.lat)                                                                            
  a.spuh(self.long)                                                                           

  dlon_rad = (b[1]-a[1]) * rad_per_deg  # Delta, converted to rad                             
  dlat_rad = (b[0]-a[0]) * rad_per_deg                                                        

  lat1_rad, lon1_rad = a.map! {|i| i * rad_per_deg }                                          
  lat2_rad, lon2_rad = b.map! {|i| i * rad_per_deg }                                          

  a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

0
推荐指数
1
解决办法
1716
查看次数