我有一个帮助方法,为资源生成"crud"链接.现在它只处理编辑和销毁.但我想重构它,以便new在传递模型类而不是实例时创建链接.
期望的输出:
> crud_links_for_resource(Product)
> { :new => '<a href="/products/new">Create a new product.</a>'}
Run Code Online (Sandbox Code Playgroud)
检查变量是模型类还是实例的最佳方法是什么?我想过使用duck typing(resource.respond_to? :new_record?)但是有更好的方法吗?
module PermissionsHelper
# Generates a hash of links to edit or destroy a resource
# @param [ActiveModel] resource
# @param [Actions] a list of crud actions to create links to
# @param [Hash] kwargs optional hash to pass to link to
# @option kwargs [String] :controller - controller name to use.
# Otherwise a guess is performed based on the …Run Code Online (Sandbox Code Playgroud) 我有一个产品模型,我需要在_form视图中写入,管理员想要插入的产品的数量.我有另一个带有Supply(产品数量)的表,所以在我的产品表中我没有属性数量,但我只有supply_id(链接我的两个产品和供应表)
由于我的产品表中没有数量,因此我在Product上使用了虚拟属性.
我不得不改变新的视图和编辑产品的原因在新的我希望字段数量但在编辑中我不想要(因为我使用另一个视图来做这个)所以,我删除了部分_form并创建了单独的视图.另外,我必须在产品的控制器中设置如果我想更新产品,我必须调用set_quantity回调,因为我必须插入一个"假"值来填充params[:product][:quantity].这是因为我在产品模型中的数量虚拟字段上设置了验证状态为true.我想知道,如果所有这些故事都是正确的(它有效,但我想要一个关于这个故事的编程设计的建议.因为我不喜欢这样一个事实,即当我有一个假值来填充数量字段时更新产品)
控制器:
class ProductsController < ApplicationController
include SavePicture
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_action :set_quantita, only: [:update]
....
def set_quantita
params[:product][:quantita]=2 #fake value for the update action
end
....
end
Run Code Online (Sandbox Code Playgroud)
模型:
class Product < ActiveRecord::Base
belongs_to :supply ,dependent: :destroy
attr_accessor :quantita
validates :quantita, presence:true
end
Run Code Online (Sandbox Code Playgroud)
如果有更好的方法来填写param[:product][:quantity]更新操作,你能说我吗?因为我不喜欢我给它的值2的事实.谢谢.
请注意,这不是一个关于向关联添加 WHERE 条件的问题,而是一个关于在使用 eager_load 时如何更改 JOIN 子句的相当高级的问题。
假设我有这些模型
class Parent
has_many :children
have_many :grades, through: :children
end
class Child
belongs_to :parent
end
class Grade
belongs_to :child
end
Run Code Online (Sandbox Code Playgroud)
因此,为了急切地加载父母,我会这样做:
Parent.eager_load(:grades)
Run Code Online (Sandbox Code Playgroud)
但如果我想要所有的父母 - 但只是渴望加载最高分,如下所示:
LEFT OUTER JOIN grades ON grades.child_id = children.id AND grades.level = 'A+'
Run Code Online (Sandbox Code Playgroud)
我尝试过使用includes(:children).joins("LEFT OUTER JOIN grades ON grades.child_id = children.id AND grades.level = 'A+'"),但由于 Rails 不构建关联对象,因此会导致对每个父级进行额外的查询。
我还没有找到任何关于使用eager_load自定义 SQL 字符串的参考资料,并且已经深入研究了源代码,但没有得到任何更明智的结果。
我正在学习RSpec,我在理解共享主题方面遇到了一些麻烦,以及它如何与新的期望语法一起工作.
我遵循了cancan wiki的教程,但我无法弄清楚如何测试用户何时无法使用相同的语法执行操作.
user_spec.rb:
require 'spec_helper'
require "cancan/matchers"
describe User do
describe 'abilities' do
let(:user) { FactoryGirl.create(:user) }
let(:ability) { FactoryGirl.create(:user) }
subject(:ability) { Ability.new(user) }
it { should be_able_to :read, User.new }
# clunky expectation
it 'should not be able to destroy others' do
expect(ability).not_to be_able_to(:destroy, User.new)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想做的就是写下一个期望
it { should not be_able_to :delete, User.new }
Run Code Online (Sandbox Code Playgroud)
我错了吗?
如何编写期望使用类的任何实例调用的消息期望?我想做这样的事情:
@controller.should_receive(:sign_in_and_redirect).with(kind_of? User)
Run Code Online (Sandbox Code Playgroud) 我有以下查询,它为每个查询获取id最新的N :observationsstation
SELECT id
FROM (
SELECT station_id, id, created_at,
row_number() OVER(PARTITION BY station_id
ORDER BY created_at DESC) AS rn
FROM (
SELECT station_id, id, created_at
FROM observations
) s
) s
WHERE rn <= #{n}
ORDER BY station_id, created_at DESC;
Run Code Online (Sandbox Code Playgroud)
我有指标的id,station_id,created_at。
这是我想出的唯一解决方案,每个站可以获取多个记录。但是,它非常慢(81000条记录的表为154.0毫秒)。
如何加快查询速度?
在minitest中仅运行控制器测试的rake命令是什么?
rake test:controller没有办法.
我想在rails app中显示以下内容:
Product instance => "product"
ProductCustomer instance => "product customer"
Run Code Online (Sandbox Code Playgroud)
所以我能做到
<%= form_for([commentable, Comment.new]) do |f| %>
<%= f.text_field :body, class: "form-control", id: "comment-text-area-#{commentable.id}",
placeholder: "Ask something about the #{commentable.class.name.split(/(?=[A-Z])/).join(' ').downcase }" %>
......
<% end %>
Run Code Online (Sandbox Code Playgroud)
目前我正在使用以下适用于所有情况的:
p = Product.new
p.class.name.split(/(?=[A-Z])/).join(' ').downcase
pc = ProductCustomer.new
pc.class.name.split(/(?=[A-Z])/).join(' ').downcase
Run Code Online (Sandbox Code Playgroud)
我的问题是它看起来太复杂了.我想应该有更好的方法.有任何想法吗?
我有一个如下所示的数组,我想从另一组中减去一组。
values1 = [[6336.94, 0, 0, 0], [3613.12, 0, 0, 0], [2862.95, 0, 0, 0]]
values2 = [[-842.68, 0, 0, 0], [-184.25, 0, 0, 0], [-112.18, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)
我想得到一个像这样的最终数组:
[[7179.62,0,0,0],[3797.37,0,0,0],[2975.13,0,0,0]]
Run Code Online (Sandbox Code Playgroud)
我已经尝试过values1.zip(values2).map {|x,y| x-y},但它返回给我一个数组,其中第一个和零被删除。
我在 ruby on Rails 中有一个模型,代码如下,它使用 singleton 类定义。另外,还有一些元编程逻辑。但是,我不明白这段代码何时会调用。是在编辑下面指定的属性时吗?
class Product < ApplicationRecord
class << self
['cat_no', 'effort', 'impact', 'effect', 'feedback'].each do |attr|
define_method "update_#{attr}" do |pr, count, user_id|
pr.order=pr.cat_no
pr.idea=pr.description
pr.update("#{attr}"=>count,:last_modified_by=>user_id)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
请帮忙。谢谢
我正在学习Swift以及Bloc.io Swiftris教程.
我为游戏板创建了一个Array2D类
// Generic arrays in Swift are actually of type struct, not class but we need a class in this case since class objects are
// passed by reference whereas structures are passed by value (copied).
// Our game logic will require a single copy of this data structure to persist across the entire game.
// Notice that in the class' declaration we provide a typed parameter: <T>.
// This allows our array to store any …Run Code Online (Sandbox Code Playgroud) 我有字符串:
str = "[[591, 184] , [741, 910] , [987,512], [2974, 174]]"
Run Code Online (Sandbox Code Playgroud)
我想将其转换为数组:
arr = [[591, 184] , [741, 910] , [987,512], [2974, 174]]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
ruby ×7
rspec ×2
activerecord ×1
arrays ×1
cancan ×1
command-line ×1
devise ×1
indexing ×1
ios ×1
minitest ×1
performance ×1
postgresql ×1
sql ×1
string ×1
swift ×1
xcode ×1