小编hju*_*ycz的帖子

Ruby on Rails 3如何使'OR'条件

我需要一个SQL语句来检查是否满足一个条件:

SELECT * FROM my_table WHERE my_table.x=1 OR my_table.y=1
Run Code Online (Sandbox Code Playgroud)

我想以'Rails 3'的方式做到这一点.我在寻找类似的东西:

Account.where(:id => 1).or.where(:id => 2)
Run Code Online (Sandbox Code Playgroud)

我知道我总是可以回退到sql或条件字符串.但是,根据我的经验,这在组合范围时经常会导致混乱.做这个的最好方式是什么?

另一个相关的问题是,如何描述依赖于OR条件的关系.我找到的唯一方法:

has_many :my_thing, :class_name => "MyTable",  :finder_sql => 'SELECT my_tables.* ' + 'FROM my_tables ' +
'WHERE my_tables.payer_id = #{id} OR my_tables.payee_id = #{id}'
Run Code Online (Sandbox Code Playgroud)

然而,当组合使用时,这些再次破裂.有没有更好的方法来指定这个?

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

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

使用生成器创建Ruby on Rails 3 gem(包括命名空间)

我正在尝试使用Rails 3(beta 4)的生成器创建一个gem.我按照这些说明操作,但我无法运行.问题是,当我在生成器文件中定义模块时,生成器会列出'rails generate',但由于找不到生成器而无法执行.

从说明中(不适用于'rails generate my_gem:install'):

module MyGem
  class InstallGenerator < Rails::Generators::Base
    source_root File.expand_path("../templates", __FILE__)

    # all public methods in here will be run in order
    def add_my_initializer
      template "initializer.rb", "config/initializers/my_gem_initializer.rb"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

修改(使用'rails generate install):

class InstallGenerator < Rails::Generators::Base
  source_root File.expand_path("../templates", __FILE__)

  # all public methods in here will be run in order
  def add_my_initializer
    template "initializer.rb", "config/initializers/my_gem_initializer.rb"
  end
end
Run Code Online (Sandbox Code Playgroud)

但是,我想为生成器设置名称空间,例如company:gem_name:generator,我必须使用模块方法(我认为).我的猜测是它与查找和目录结构有关,但我无法弄清楚如何.我尝试了几种方法:

lib
-generators
--my_gem.rb

lib
-generators
--company
---my_gem.rb

lib
-generators
--company
---my_gem_name
----my_gem.rb
Run Code Online (Sandbox Code Playgroud)

但没有任何帮助.我在互联网上也发现了很多,但是如果我展示了我需要的东西,那就是非.

ruby rubygems ruby-on-rails-plugins ruby-on-rails-3

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

Psych YAML解析包括评论

根据http://yaml.org/spec/current.html#id2509980,YAML文件中的注释是一个表示性细节,不得在序列化/表示图中(http://yaml.org/spec/current.html#代表/).看起来Psych正在根据规范进行解析并丢失注释,这意味着当文件包含注释时,无法解析YAML文件并将其再次序列化.在我看来这很奇怪,因为评论在这样的文件中很重要(例如配置).

有没有人知道是否可以用现有的库解析评论,或者是我自己完成这一切的唯一方法?

ruby yaml psych

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

使用Spring/Jetty(或Tomcat)/ Maven进行REST测试的集成测试

我想用HTTP请求测试我们的REST服务(有点黑盒测试).然而,经过几个小时的谷歌搜索和尝试不同的配置,我仍然无法正确启动Jetty.这是我当前的配置(我尝试了多个不同的版本):

的pom.xml

        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <junitArtifactName>junit:junit</junitArtifactName>
                <excludes>
                    <exclude>**/*_Roo_*</exclude>
                    <exclude>**/*Util*</exclude>
                    <exclude>**/*IT*</exclude>
                </excludes>
                <forkMode>once</forkMode>
                <argLine>-javaagent:'${settings.localRepository}/org/springframework/spring-instrument/${org.springframework-version}/spring-instrument-${org.springframework-version}.jar' -javaagent:'${settings.localRepository}/org/aspectj/aspectjweaver/${org.aspectj-version}/aspectjweaver-${org.aspectj-version}.jar'</argLine>
                <useSystemClassLoader>true</useSystemClassLoader> 
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <forkMode>once</forkMode>
                <argLine>-javaagent:'${settings.localRepository}/org/springframework/spring-instrument/${org.springframework-version}/spring-instrument-${org.springframework-version}.jar' -javaagent:'${settings.localRepository}/org/aspectj/aspectjweaver/${org.aspectj-version}/aspectjweaver-${org.aspectj-version}.jar'</argLine>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>  
            <groupId>org.mortbay.jetty</groupId>  
            <artifactId>maven-jetty-plugin</artifactId>  
            <version>6.1.10</version>  
            <configuration>  
                <scanintervalseconds>10</scanintervalseconds>  
                <stopkey>foo</stopkey>  
                <stopport>9999</stopport>  
                <contextpath>/${project.artifactId}</contextpath>  
                <connectors>  
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                        <port>9090</port>  
                    </connector>  
                </connectors>  
            </configuration>  
            <executions>  
                <execution>  
                    <id>start-jetty</id>  
                    <phase>pre-integration-test</phase>  
                    <goals>  
                        <goal>run</goal>  
                    </goals>  
                    <configuration>  
                        <scanintervalseconds>0</scanintervalseconds>  
                        <daemon>true</daemon>  
                    </configuration>  
                </execution>  
                <execution>  
                    <id>stop-jetty</id>  
                    <phase>post-integration-test</phase>  
                    <goals>  
                        <goal>stop</goal>  
                    </goals>  
                </execution> …
Run Code Online (Sandbox Code Playgroud)

rest spring integration-testing maven-jetty-plugin load-time-weaving

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