Rails:对集合而不是数据库表使用现有的模型validation规则

Rails 4,Mongoid而不是ActiveRecord(但是为了这个问题,这应该改变任何东西)。 假设我有一个带有一些validation规则的MyModel域类: class MyModel include Mongoid::Document field :text, type: String field :type, type: String belongs_to :parent validates :text, presence: true validates :type, inclusion: %w(ABC) validates_uniqueness_of :text, scope: :parent # important validation rule for the purpose of the question end 其中Parent是另一个域类: class Parent include Mongoid::Document field :name, type: String has_many my_models end 此外,我在数据库中的相关表填充了一些有效数据。 现在,我想从CSV文件导入一些数据,这可能与数据库中的现有数据冲突。 最简单的方法是为CSV中的每一行创建一个MyModel实例,并validation它是否有效,然后将其保存到数据库中(或丢弃它)。 […]

在Rails ActiveRecord Query中返回自定义列

我使用以下内容查询rails中的ActiveRecords: result = MyObj.where({customer: current_id}).as_json() 返回了两列: result = [{id:1, name: “david”, last_name: “Smith:”}] 我想创建第三列(不会保存到数据库),如下所示: result = [{id:1, name: “David”, last_name: “Smith:”, full_name:”David Smith”}] 这可能在WHERE查询中吗?

Rails需要RubyGems> = 0.9.4。 请安装RubyGems

我正在使用Rails 2.1.0(来自gem )部署到slicehost上的Ubuntu slice 如果我尝试mongrel_rails start或脚本/服务器,我会收到此错误: Rails requires RubyGems >= 0.9.4. Please install RubyGems 当我键入gem -v我安装了1.2.0版本。 关于要修复什么的快速提示?

“拒绝访问” – 用户对S3 Bucket的权限

我发送了一封自动发送电子邮件的订单,现在我正试图找到一个下载PDF收据的链接。 用户在尝试下载时在浏览器中收到错误“此XML文件似乎没有与之关联的任何样式信息。文档树如下所示。” 我已经进入水桶并在收据桶上运行“公开”,仍然没有运气。 文件结构是: 应用程序名称/上传/顺序/收据 我需要做什么才能允许用户下载收据的权限?

Rails 3.1:通过关系在has_many的连接表上自定义validation消息?

我有一个Meal模型has_many :foods, :through => :servings 。 Meal还: accepts_nested_attributes_for :servings, :allow_destroy => true validates_associated :servings Serving模型有一个名为serving_amount的字段和一个名为amount_recorded的字段。 在Serving模型中,我目前使用此validation: validates :serving_size, :numericality => {:greater_than => 0}, :if => :amount_recorded? validation失败时返回的消息很糟糕。 事实上,每餐可能有多份食物。 如何创建引用服务大小无效的食物名称的自定义validation消息? 例如,我想说,“你为西瓜输入了一个无效的服务大小”,如果amount_recorded为一个name西瓜的Food的服务是真的,validation失败。

Ruby数组不合作

可能重复: 什么时候ruby的Array.slice应该返回nil? 大家好……为什么当[5,1]返回[]并且[6,1]在两个起始元素明显超出范围时返回nil ? irb(main):048:0> a = [ “a”, “b”, “c”, “d”, “e” ] => [“a”, “b”, “c”, “d”, “e”] irb(main):049:0> a[0] => “a” irb(main):050:0> a[4] => “e” irb(main):051:0> a[5,1] => [] irb(main):052:0> a[6,1] => nil 谢谢!

问题包括Mandrill Mailer和Rails中的日历附件

我目前正在使用icalendar gem创建一个新的日历,然后通过mandrill_mailer gem作为附件发送它。 我尝试了各种不同的方法 – 到目前为止我相信我最接近: Event.rb require ‘base64’ def self.export_events(user) @event = Event.last @calendar = Icalendar::Calendar.new event = Icalendar::Event.new event.summary = @event.title event.dtstart = @event.start_time.strftime(“%Y%m%dT%H%M%S”) event.dtend = @event.end_time.strftime(“%Y%m%dT%H%M%S”) event.description = @event.desc event.location = @event.location @calendar.add_event(event) encoded_cal = Base64.encode64(@calendar.to_ical) CalendarMailer.send_to_ical(user, encoded_cal).deliver end calendar_mailer.rb class CalendarMailer < MandrillMailer::TemplateMailer default from: "blah@blah.com" # iCal def send_to_ical(user, encoded_cal) […]

Mongoid排序模型基于数组大小,在其他Model(has_one)关系中

我有Postactivity模型,它有post_id作为Post Model的外键(has_one关系),这个Postactivity模型有Postactivity的数组。 我如何按likes对Post模型进行排序? class Post has_one :postactivity, foreign_key: :post_activity_id, class_name:”PostActivity” end class PostActivity field :likes, type: Array belongs_to :post, foreign_key: :post_id, class_name: “Post” end

Rails加载除application.css.scss之外的所有样式表

我正在使用Rails书籍跟踪敏捷Web开发,并遇到了一个问题。 除application.css.scss外,assets文件夹中的所有样式表都正确加载 运行bundle exec rake assets:precompile将所有资产上的副本bundle exec rake assets:precompile到公共文件夹,这似乎有效,但我不想每次都这样做。 在application.html.erb中: Pragprog Books Online Store “all” %> <body class='’> Home Questions News Contact 在application.css.scss中: /* * This is a manifest file that’ll be compiled into application.css, which will * include all the files listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, […]

使用google_api_client gem在用户的Feed上看不到Google +时刻

我正在尝试使用google api客户端发布到用户的流,我使用下面提到的代码 require ‘google/api_client’ require ‘google/api_client/client_secrets’ require ‘google/api_client/auth/installed_app’ client = Google::APIClient.new client.authorization.client_id = CLIENT_ID client.authorization.client_secret = CLIENT_SECRET client.authorization.access_token = USER_ACCESS_TOKEN plus = client.discovered_api(‘plus’, ‘v1’) moment = { :type => ‘http://schemas.google.com/AddActivity’, :target => { :id => Time.now.to_i.to_s, :description => ‘well this is it’, :name => ‘Well this is it’ } } req_opts = { :api_method => […]