小编use*_*706的帖子

Rails迁移更改vs Up&Down方法

我正在阅读Rails Test Prescriptions一书,在设置过程中,它要求我将迁移文件更改为以下内容:

class ProjectUserJoin < ActiveRecord::Migration
  def self.up
    create_table :projects_users, :force => true, :id => false do |t|
      t.references :project
      t.references :user
      t.timestamps
    end
  end

  def self.down
    drop_table :projects_users
  end
end
Run Code Online (Sandbox Code Playgroud)

看起来我在Rails(4.0.0)上使用的是比书(2或3.x)更高的版本,我的迁移文件如下所示:

class ProjectUserJoin < ActiveRecord::Migration
  def change
  end
end
Run Code Online (Sandbox Code Playgroud)

如何编辑更改方法以执行与上述上下方法相同的操作?到目前为止,我尝试使用up和down而不是self.up和self.down并使用相同的代码进行复制.那没起效.

谢谢!

ruby ruby-on-rails rails-migrations rails-activerecord

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

JavaScript - 如何更改字符串的元素?

JS新手在这里.我想编写一个基于条件更改字符串中每个元素的基本程序.如果字母是大写的,我们将它换成小写,如果字母已经是小写,我们将它换成大写.为什么这不起作用?谢谢!

function SwapCase(str){

for (var i = 0; i < str.length; i++) {
    if (str.charAt(i)===str.charAt(i).toUpperCase()) {
       str.charAt(i).toLowerCase();
    } else{}
       str.charAt(i).toUpperCase();
    }

return str;

}

SwapCase("gEORGE");
Run Code Online (Sandbox Code Playgroud)

javascript

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

在 WordPress 中获取帖子摘录

我试图让这个 WordPress 页面模板显示特定帖子的摘录。创建帖子后,我确定将链接插入到我想要的位置。我能够获取标题、缩略图、永久链接等......但出于某种原因我无法获取摘录。我努力了:

the_excerpt();
get_the_excerpt();
the_content('',FALSE);
get_the_content('', FALSE, '');
get_the_content('', TRUE);
Run Code Online (Sandbox Code Playgroud)

除其他事项外。当我尝试时,get_the_content('', TRUE)它会为我提供链接之后所有内容的内容,但我想要链接之前的内容。

有任何想法吗?

   <?php
        $query = 'cat=23&posts_per_page=1';
        $queryObject = new WP_Query($query);
    ?>

    <?php if($queryObject->have_posts()) : ?>

        <div>

            <?php while($queryObject->have_posts()) : $queryObject->the_post() ?>

                <div>

                    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

                    <br>

                    <?php the_post_thumbnail() ?>

                    <?php #the_excerpt(); ?>

                    <div>

                        <a href="<?php the_permalink(); ?>">Read More</a>

                    </div>

                </div>

            <?php endwhile ?>

        </div>

    <?php endif; wp_reset_query();
Run Code Online (Sandbox Code Playgroud)

?>

php wordpress

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

为这个问题找到一个算法难倒:找到不属于的集合

给定一组集合,找出不属于的集合:
例如:[[a,b,c,d], [a,b,f,g], [a,b,h,i], [j,k ,l,m]]
输出:[j,k,l,m]

我们可以在上面看到前三个集合有一个公共子集 [a,b] 而最后一个没有。注意:可能存在异常集确实包含输入组中包含的元素的情况。在这种情况下,我们必须找到与其他集合的共同点最少的集合。

我尝试遍历输入列表并保留每个字符的计数(在哈希中)。
在第二遍中,找出总数最小的集合。
在上面的示例中,最后一组的计数总和为 4:
j*1 + k*1 + l*1 + m*1。

我想知道是否有更好的方法来做到这一点。

arrays algorithm set

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