我正在寻找一个函数,它返回序列中的第一个元素,其中fn的计算结果为true.例如:
(first-map (fn [x] (= x 1)) '(3 4 1))
Run Code Online (Sandbox Code Playgroud)
上面的假函数应该返回1(列表中的最后一个元素).在Clojure中有类似的东西吗?
我们使用Symfony2的角色功能来限制用户访问我们应用的某些部分.我们的每个用户实体都有许多具有开始日期和结束的订阅实体,用户可以购买年度订阅.
现在,有没有办法根据用户是否拥有"有效"订阅动态添加角色?在rails中,我只是让模型处理它是否具有必要的权限,但我知道通过设计symfony2实体不应该访问Doctrine.
我知道你可以从一个实体实例中访问一个实体的关联,但是它会遍历所有用户的订阅对象,这对我来说似乎不那么麻烦.
我对测试和rails非常陌生,并试图自己解决这个问题,但没有运气.
我有以下型号
class Picture < ActiveRecord::Base
belongs_to :product
has_attached_file :image
end
class Product < ActiveRecord::Base
has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures, :reject_if => lambda { |p| p[:image].blank? }, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
和一个相当标准的控制器,我猜......
def create
@product = Product.new(params[:product])
if @product.save
redirect_to products_path, :notice => "blah."
else
render :action => "new"
end
end
Run Code Online (Sandbox Code Playgroud)
我将如何进行测试呢?我试过这样的东西,但是我无法让它起作用:
describe ProductsController do
it "adds given pictures to the product" do
product = Factory.build(:product)
product.pictures.build(Factory.attributes_for(:picture))
post :create, :product => product.attributes
Product.where(:name => product[:name]).first.pictures.count.should == 1 # …Run Code Online (Sandbox Code Playgroud)