我有vector<vector<int> > Y
.我想将Y中的子向量(称为y)合并为一个vector<int>
.但我不想对它们进行排序,即按照它们出现的顺序合并它们.我怎么能有效地做到这一点,也许是通过使用STL算法?该std::merge
方法通过排序合并我不想要的.
编辑:我想要的是:给定{{1,6,5},{5,3-1,77},{0},...}返回{1,6,5,5,3,-1, 77,0,...}
我在rails(4.2.6)中使用devise(4.2.0).
在我的应用程序中,我在用户和配置文件表中使用嵌套属性.我只有在创建新记录时才需要验证密码,在更新创建的记录时未验证密码字段.
我的user.rb文件是:
class User < ActiveRecord::Base
has_one :profile
has_one :company_profile
accepts_nested_attributes_for :profile
attr_accessor :profile_updation
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :email, if: :email_required?
validates_presence_of :password, if: :password_required?
validates_confirmation_of :password, if: :password_required?
protected
def email_required?
true && profile_updation.blank?
end
def password_required?
!password.nil? || !password_confirmation.nil?
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时出现此错误:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email" DETAIL: Key (email)=() already exists. : INSERT INTO "users" ("first_name", "last_name", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) …
Run Code Online (Sandbox Code Playgroud) 使用之间的确切区别是什么:
1- gem install [gemname]
和
2-为GemFile添加gem名称和版本并运行bundle install
?
我正在运行一个ActiveRecord
返回一堆记录的查询。
让我们以此为例
ad_account campaign_id impressions
1 1 500
1 2 8,800
1 3 6,000
2 7 900
2 9 9,324
Run Code Online (Sandbox Code Playgroud)
如何将这些结果转换为包含按 ad_account 分组的数组的哈希值?
所以我想要这个
{"1"=>[{ad_account: 1, campaign_id: 1, impressions: 500}, {ad_account: 1, campaign_id: 2, impressions: 8,800}, {ad_account: 1, campaign_id: 3, impressions: 6,000}],
"2"=>[{ad_account: 2, campaign_id: 7, impressions: 900}, {ad_account: 2, campaign_id: 9, impressions: 9324}]}
Run Code Online (Sandbox Code Playgroud)
所以基本上每个ad_account
都是键,值是所有字段的数组。
我该怎么做呢?
我已经尝试过,但它不起作用,因为它只是覆盖
allCampaigns.each do |campaign|
hash[campaign.ad_account] = campaign
end
Run Code Online (Sandbox Code Playgroud) 在mackenziechild-recipe_box的第3周,我遇到了一些关于Cocoon的问题.我已经设计安装,以及和我ingredients
和directions
属性,当我创建一个新的未保存Recipe
.但是当我更新现有产品时Recipe
,一切都很好.错误消息是:
配料配方必须存在,方向配方必须存在
我究竟做错了什么?我正在使用rails 5
应用程序/模型/ recipe.rb
class Recipe < ApplicationRecord
validates :title, :description, :image, presence: true
has_attached_file :image, styles: { medium: "400x400#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
belongs_to :user
has_many :ingredients
has_many :directions
accepts_nested_attributes_for :ingredients, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :directions, :reject_if => :all_blank, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器/ recipes_controller.rb
def new
@recipe = Recipe.new
@recipe = current_user.recipes.build
end
def create
@recipe = Recipe.new(recipe_params)
@recipe = current_user.recipes.build(recipe_params)
if @recipe.save
# …
Run Code Online (Sandbox Code Playgroud) Model.limit(6).offset(5)
当我们必须从第6个索引开始获得6个元素时,我不明白这样做的原因.我认为应该是Model.offset(5).limit(6)
因为我们首先必须告诉我们必须从哪个索引开始检索元素,然后将搜索限制为作为参数传递给限制的元素数量,而不是反过来.
我正在使用Ruby 2.4.如果我有一个有序的数字数组,比方说
[1, 2, 4, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
如何找到数组中不存在的数值元素,数组中最小值和最大值之间的数值?例如,在上面,缺失值是
[3, 5, 6]
Run Code Online (Sandbox Code Playgroud)
如果我的阵列是
[2, 7]
Run Code Online (Sandbox Code Playgroud)
我想要的缺失值是
[3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud) 当我进入文章/编辑时,我收到一个错误.
当我应该得到这个错误:
我的代码:
articles_controller:
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
flash[:notice] = "Article was submitted succsefully"
redirect_to (@article)
else
render :new
end
end
def show
@article = Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title, :description)
end
end
Run Code Online (Sandbox Code Playgroud)
我的edit.html.erb:
<h1>Edit the existing article</h1>
<% if @article.errors.any? %>
<h2>The following errors are informing you that if you don't do these then
your articles will not …
Run Code Online (Sandbox Code Playgroud) 我有这个哈希数组:
@fournisseurs = [
{ id: 10592,
nom: 'Carrossier Procolor Armand-Paris (Garage Michel Tondreau)',
distance: '3.9 km',
dispos: nil
},
{ id: 10463,
nom: 'Carrossier Procolor Grand Beauport(Garage Michel Tondreau)',
distance: '3.8 km',
dispos: nil
},
{ id: 10594,
nom: 'Honda Charlesbourg',
distance: '5.2 km',
dispos: nil
},
{ id: 10508,
nom: 'Carrossier ProColor Charlesbourg',
distance: '15.5 km',
dispos: nil
}]
Run Code Online (Sandbox Code Playgroud)
我想按距离对它进行排序.我试过了@fournisseurs.sort_by! { |fournisseur| fournisseur[:distance]}
,但它并没有对我的哈希数组进行排序.我读到这sort_by!
是不稳定的.我怎样才能做到这一点?
提前致谢 !
我有这个哈希:
{
:items => {
:"cust.category" => "Jackets,Belts & Buttons",
:"items.receive_dt" => "2 years",
:"prdcts.brand" => "Armani"
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试更改此哈希使其变为->
{
:items => ["cust.category=Jackets,Belts & Buttons",
"items.receive_dt=2 years",
"prdcts.brand=Armani"]
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几件事,但无法正确处理