小编use*_*382的帖子

Rails:基于关联值的ActiveRecord查询

我有2个型号.Report并且Server有一个belongs_to和has_many关系.我创建了一个使用的访问器方法delegate,允许Report找到它的关联Server.company_id.现在,我想运行一个查询Report,允许我查找ReportServer具有特定company_id属性5 的特定属性相关联的所有内容.

这是我的两个型号.是的,我知道当前的查询不起作用,因为Report没有属性company_id.

不,我不想存储company_id内部,Report因为该信息不属于Report.

报告

class Report < ActiveRecord::Base

 belongs_to :server

 delegate :company_id, :to => :server

    class << self

        def method(url, base_url)
            #Report.where(company_id: 5)
        end
    end

end
Run Code Online (Sandbox Code Playgroud)

服务器

class Server < ActiveRecord::Base

 attr_accessible :company_id

 has_many :reports

end
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails associations

54
推荐指数
3
解决办法
4万
查看次数

如何使用邮差拦截器

自从我升级到Postman 0.9.6后,我无法复制用户会话,因为chrome不允许邮递员获取用户cookie.然而,他们决定通过创建另一个名为Postman Interceptor的插件来"解决"这个问题,该插件可以路由你的邮递员请求并抓取用户cookie.他们在这里写了一篇关于这个奇妙工具的博客文章:http://blog.getpostman.com/index.php/2014/02/11/postman-v0-9-6-access-cookies-and-restricted-headers-plus -更好的测试/

但是,我似乎无法弄清楚如何使这个东西工作.我现在已经尝试了一个小时,它仍然不会将我的请求视为登录用户

cookies google-chrome google-chrome-devtools postman

54
推荐指数
3
解决办法
6万
查看次数

Capybara:无法找到css

我使用capybara点击一个复选框,但无论我做什么,它似乎都找不到它.我能够正确地找到跨度内的跨度和标签,但不能找到我实际需要的输入.

这是复选框

<span class="checkbox tos">
  <input id="agreement" name="agreement" onclick="agreeValidate();" type="checkbox" value="false">
  <label for="agreement">I accept the <a href="http://www.dev.com:3000/terms" target="_blank">Terms of Use</a>, <a href="http://www.dev.com:3000/privacy" target="_blank">Privacy Policy</a>, and am at least 13 years old</label>
</span>
Run Code Online (Sandbox Code Playgroud)

以下是我尝试过的一些事情

page.check('agreement')
find(:css, '#agreement').set(true)
find('#agreement').set(true)
find('#agreement').click
Run Code Online (Sandbox Code Playgroud)

但是,他们都给了我同样的错误

Unable to find css "#agreement" (Capybara::ElementNotFound)
Run Code Online (Sandbox Code Playgroud)

我也想知道onclick当单击复选框时,这些方法中的任何一个都会触发该方法吗?我觉得find(:css, '#agreement').set(true)不会触发onclick事件.但是,我不确定其余的事情.

更新

我也试过通过xpath选择元素.以下是我发现的各种事情

find(:xpath, '//*[@id="registration"]/span[2]')
Run Code Online (Sandbox Code Playgroud)

这样就可以找到span没有问题的元素

find(:xpath, '//*[@id="registration"]/span[2]/input')
Run Code Online (Sandbox Code Playgroud)

这找不到我需要的元素,但是这个xpath正确地选择了chrome控制台中的元素

find(:xpath, '//*[@id="agreement"]')
Run Code Online (Sandbox Code Playgroud)

这找不到我需要的元素,但xpath选择chrome控制台中的元素

find(:xpath, '//*[@id="registration"]/span[2]/label')
Run Code Online (Sandbox Code Playgroud)

这样就可以labelspan没有问题的情况下找到元素.

rspec capybara capybara-webkit

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

如何使用JPA和Hibernate在非主键上连接表

我有3种型号User,House,UserHouseMap.我需要通过地图访问用户的房子.唯一的问题是这是一个旧的DB我不能改变我需要映射UserUserHouseMap使用的事实user.name,这是一个非主键.

Hibernate一直给我错误说我需要将它作为主键或者我得到错误说 A JPA error occurred (Unable to build EntityManagerFactory): Unable to find column with logical name: name in org.hibernate.mapping.Table(users) and its related supertables and secondary tables

我尝试@Formula过一种解决方法,但是没有用.我也试过,@JoinColumnOrFormula但也没有用.这是我的解决方案@Formula

@Expose
@ManyToOne(targetEntity = House.class)
@Formula("(select * from houses inner join user_house_map on houses.house_name = user_house_map.house_name where user_house_map.user_name=name)")
public House house;
Run Code Online (Sandbox Code Playgroud)

这是我尝试@JoinColumnOrFormula解决方案.

@Expose
@ManyToOne(targetEntity = House.class)
@JoinColumnsOrFormulas({
        @JoinColumnOrFormula(formula=@JoinFormula(value="select name from users where users.id= id", …
Run Code Online (Sandbox Code Playgroud)

java orm hibernate jpa hibernate-mapping

28
推荐指数
1
解决办法
2万
查看次数

Rails:form_for复选框设置为true或false是否选中/取消选中该框

我有一个名为users的模型,它有2个布尔属性send_emailsend_text.我有一个用于编辑User模型的表单,我希望它将这些属性设置为true/false,具体取决于是否选中该复选框.这是我的表格

<%= form_for(@user) do |f| %>
    <div class="field">
        <%= f.label :email %> <br />
        <%= f.text_area :email %> <br />
    </div>
    <div class="field">
        <%= f.label :cell %> <br />
        <%= f.text_area :cell %> <br />
    </div>
    <div class="field">
        <%= f.label "Get Email" %> <br />
        <%= f.check_box :send_email, {}, true, false %> <br />
    </div>
    <div class="field">
        <%= f.label "Get Text" %> <br />
        <%= f.check_box :send_text, {}, true, false %> <br />
    </div>
    <div class="actions"> …
Run Code Online (Sandbox Code Playgroud)

html ruby forms ruby-on-rails

25
推荐指数
1
解决办法
7万
查看次数

Psycopg2 Python SSL支持未编译

我正在尝试使用psycopg2与sslmode ='required'参数连接到我的postgres数据库; 但是,我收到以下错误

psycopg2.OperationalError: sslmode value "require" invalid when SSL support is not compiled in
Run Code Online (Sandbox Code Playgroud)

这是关于我的系统的几个细节

  • Mac OS X El Capitan
  • Python 2.7
  • 通过pip安装psycopg2
  • 通过自制软件安装python

这是我试图解决问题的方法

  • brew uninstall python
  • which python仍然显示python生活在/usr/local/bin/python,试图卸载这个但不能.并且听说这是操作系统使用的python,不管怎么说都不应该卸载
  • brew install python --with-brewed-openssl --build-from-source
  • pip uninstall psycopg2
  • pip install psycopg2

完成所有这些后,异常仍然发生.我通过运行这个python脚本#!/usr/bin/env python不知道它的问题,但是这是一个不同的目录比一个which python节目

python postgresql ssl psycopg2 python-2.7

17
推荐指数
4
解决办法
9782
查看次数

具有多个参数和模型访问的Rake任务不起作用

运行我的rake任务时出现以下错误

rake store_reports["1", "2"]
rake aborted!
Don't know how to build task 'store_reports[1,'
Run Code Online (Sandbox Code Playgroud)

我的rake任务需要2个参数,需要访问其中的模型.这是任务

task :store_reports, [:start_date, :end_date] => :environment do |t, args|
    puts args.start_date
end
Run Code Online (Sandbox Code Playgroud)

我引用了两个stackoverflow问题,但第一个答案没有用,在第二个答案中,作者似乎已经解决了,但他从未发布过他的答案.

耙子流产了!undefined方法`map'for:name: 带有多个参数的符号rake任务 - 我卡住了

这是一些额外的信息.我跑的地方,rake -T我看不到我的佣金任务

ruby rake ruby-on-rails ruby-on-rails-3

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

Rspec/FactoryGirl:清理数据库状态

我是Rspec和Factory女孩的新手,希望我的测试能够在特定的数据库状态下运行.我知道我可以让Factory girl创建这些记录,并且在测试运行后对象将被销毁,但是如果我在数据库中有数据会发生什么.

例如:我希望我的测试在我通过Factory Girl创建的数据库中有3条记录时运行.但是,我目前在数据库中已有1个模型记录,我不想仅为测试删除它.那里有一个模型毁了我的测试.

数据库内容

[#<Leaderboard id: 1, score: 500, name: "Trudy">]
Run Code Online (Sandbox Code Playgroud)

leaderboard_spec.rb

require 'spec_helper'

describe Rom::Leaderboard do

    describe "poll leaderboard" do
        it "should say 'Successful Run' when it returns" do
            FactoryGirl.create(:leaderboard, score: 400, name: "Alice")
            FactoryGirl.create(:leaderboard, score: 300, name: "Bob")
            FactoryGirl.create(:leaderboard, score: 200, name: "John")
            Leaderboard.highest_scorer.name.should == "Alice"
        end
    end

end
Run Code Online (Sandbox Code Playgroud)

现在我的测试将失败,因为它会错误地认为Trudy是最高得分手,因为测试运行的状态不正确.

工厂女孩是否提供从数据库中删除记录然后回滚此删除?类似于它在数据库中创建记录和回滚的方式

rspec ruby-on-rails ruby-on-rails-3 factory-bot

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

Dropwizard Metric Annotations @Timed无效

我正在尝试使用@Timed(http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/annotation/package-summary.html)等注释自动将指标发布到我的MetricRegistry .

这不是开箱即用的.在搜索问题时,我发现了Codahale Metrics:在普通Java使用@Timed指标注释,其中提到了这个工作的唯一方法是使用aspectj.我将此添加到我的项目中,但仍未在MetricRegistry中看到我的指标.

这是我的pom文件.我添加了一个librato库,它加载了com.codahale.metrics:metrics-annotation.

<dependency>
  <groupId>io.astefanutti.metrics.aspectj</groupId>
  <artifactId>metrics-aspectj</artifactId>
  <version>${metrics-aspectj.version}</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.10</version>
</dependency>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
      <showWeaveInfo>true</showWeaveInfo>
      <source>1.8</source>
      <target>1.8</target>
      <complianceLevel>1.8</complianceLevel>
      <encoding>UTF-8</encoding>
      <verbose>true</verbose>
      <aspectLibraries>
        <aspectLibrary>
          <groupId>io.astefanutti.metrics.aspectj</groupId>
          <artifactId>metrics-aspectj</artifactId>
        </aspectLibrary>
      </aspectLibraries>
    </configuration>
    <executions>
      <execution>
        <phase>process-sources</phase>
        <goals>
          <goal>compile</goal>
          <goal>test-compile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

<dependency>
  <groupId>com.librato.metrics</groupId>
  <artifactId>metrics-librato</artifactId>
  <version>${metrics-librato.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这就是我尝试使用指标的方式

@Metrics(registry = "default") // this.metricRegistry is default
public class Foo {
    @Inject
    private MetricRegistry metricRegistry;
    ...

    @Metered(name = "meterName")
    public void bar() { …
Run Code Online (Sandbox Code Playgroud)

java aspectj java-8 dropwizard librato

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

Ruby on rails:使用belongs_to关联创建模型条目

我正在尝试在我的数据库中为具有belongs_to关系的模型添加新条目.我有两个模型,乔布斯和客户.

很容易找到关于如何在这两者之间建立关联的教程(使用has_many和belongs_to),但我似乎找不到实际使用关联的任何示例.

在我的代码中,我正在尝试为第一个客户创建一个新工作.作业模型有一个client_id的属性,我知道我可能只是手动填充属性,但必须有一些ruby约定来轻松实现这一点.

Job.create(:client_id => 1, :subject => "Test", :description => "This is a test")
Run Code Online (Sandbox Code Playgroud)

我可以轻松地将它放在我的代码中,但我觉得ruby有更好的方法来做到这一点.这是我的模型设置方式

class Job < ActiveRecord::Base
  attr_accessible :actual_time, :assigned_at, :client_id, :completed_at, :estimated_time, :location, :responded_at, :runner_id, :status, :subject, :description
  belongs_to :client
end

class Client < User
    has_many :jobs
end

class User < ActiveRecord::Base
  attr_accessible :name, :cell, :email, :pref

end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails associations ruby-on-rails-3

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