Tag: activerecord

Rails嵌套属性关联validation失败

我有一个Rails模型的嵌套属性,并且关联validation由于某种原因失败。 我没有使用accepts_nested_attributes_for,但我正在做一些非常相似的事情。 class Project “name”, :value=>val) end end end class ProjectAttribute :project_id validates_presence_of :project_id, :unless => lambda {|attribute| attribute.project.try(:valid?)} validates_associated :project end 这是一个人为的例子,但与我试图做的类似。 我已经看了一下accepts_nested_attributes_for做了什么,它使用了构建方法,我认为它会将构建的属性与项目相关联。 我还查看了accepts_nested_attributes_for子关联validation失败 ,它给了我validates_presence_of :unless=>valid? 有关如何使其工作的任何想法?

连接池问题与rufus-scheduler中的ActiveRecord对象有关

我正在使用rufus-scheduler来运行一些频繁的作业,这些作业使用ActiveRecord对象执行一些不同的任务。 如果存在任何类型的网络或postgresql打嗝,即使在恢复之后,所有线程都会抛出以下错误,直到重新启动该进程: ActiveRecord :: ConnectionTimeoutError(无法在5秒内获得数据库连接(等待5.000122687秒)。最大池大小当前为5;考虑增加它。 重新启动postgres可以很容易地重现错误。 我已经尝试过玩游戏(最多15个),但是没有运气。 这让我相信连接只处于陈旧状态,我认为可以通过调用clear_stale_cached_connections!来解决这个clear_stale_cached_connections! 。 有没有更可靠的模式来做到这一点? 传递的块是一个简单的选择和更新活动记录调用,并且恰好与AR对象有关。 rufus工作: scheduler.every ‘5s’ do db do DataFeed.update #standard AR select/update end end 包装: def db(&block) begin ActiveRecord::Base.connection_pool.clear_stale_cached_connections! #ActiveRecord::Base.establish_connection # this didn’t help either way yield block rescue Exception => e raise e ensure ActiveRecord::Base.connection.close if ActiveRecord::Base.connection ActiveRecord::Base.clear_active_connections! end end

轨道中的多个表连接

我如何在mysql查询下写入rails activerecord select A.*, B.* from raga_contest_applicants_songs AS A join raga_contest_applicants AS B ON B.contest_applicant_id=A.contest_applicant_id join raga_contest_rounds AS C ON C.contest_cat_id=B.contest_cat_id WHERE C.contest_cat_id = contest_cat_id GROUP BY C.contest_cat_id 我知道如何在两个表上编写连接,对如何在3个表上使用连接不太自信。

Rails:拥有并且属于许多(HABTM) – 创建关联而不创建其他记录

在Google上花了一整天,但找不到答案。 :\ 我在用户和Core_Values之间有HABTM关系。 class CoreValue < ActiveRecord::Base has_and_belongs_to_many :users class User < ActiveRecord::Base has_and_belongs_to_many :core_values 在我的控制器中,我需要做两件事: 如果CoreValue不存在,请创建一个新的并将其与给定的用户ID相关联,并且 假设我知道特定的CoreValue确实存在,则创建关联而不创建任何新的CoreValues或Users 对于#1,我有这个工作: User.find(current_user.id).core_values.create({:value => v, :created_by => current_user.id}) 这将创建一个新的CoreValue:value和:created_by并创建关联。 对于#2,我尝试了一些东西,但似乎不太可能只创建关联。 谢谢你的帮助!

Rails:从现有表创建模型?

我已经从一个不同的项目创建了表。 它们的名称格式为aaa_bbb_ccc_ddd(全部为非复数,部分部分不是常规字)。 通过阅读本文,我已经从数据库中成功创建了一个模式。 但现在我必须制作实际模型。 我看过RMRE ,但他们在我的表上强制执行A​​ctiveRecord约定并更改其名称,我不想这样做,因为其他应用依赖于这些表。 从现有表自动创建模型和模式的最佳方法是什么?

为字段创建PostgreSQL序列(不是记录的ID)

我正在开发Ruby on Rails应用程序。 我们正在使用PostgreSQL数据库。 有一个名为scores的表,其中包含以下列: Column | Type ————–+———————– id | integer value | double precision ran_at | timestamp active | boolean build_id | bigint metric_id | integer platform_id | integer mode_id | integer machine_id | integer higher_better | boolean job_id | integer variation_id | integer step | character varying(255) 我需要在job_id添加一个序列 (注意:没有job模型)。 如何创建此序列?

Rails获取上一个和下一个活动记录对象的最佳方式

我需要使用Rails获取上一个和下一个活动记录对象。 我做到了,但不知道这是否是正确的方法。 我得到了什么: 控制器: @product = Product.friendly.find(params[:id]) order_list = Product.select(:id).all.map(&:id) current_position = order_list.index(@product.id) @previous_product = @collection.products.find(order_list[current_position – 1]) if order_list[current_position – 1] @next_product = @collection.products.find(order_list[current_position + 1]) if order_list[current_position + 1] @previous_product ||= Product.last @next_product ||= Product.first product_model.rb default_scope -> {order(:product_sub_group_id => :asc, :id => :asc)} 所以,这里的问题是我需要进入我的数据库并获取所有这些ID以了解谁是前一个和下一个。 尝试使用gem order_query ,但它对我不起作用,我注意到它进入数据库并按顺序获取所有记录,所以,这就是为什么我做了同样但只得到id。 我找到的所有解决方案都是简单的订单查询。 按ID排序或类似优先级字段。

在Rails测试中更新ActiveRecord

我正在编写我的第一个Rails’Store’应用程序,我在我的一个测试中遇到了一些奇怪的东西。 我正在尝试在cart.rb上测试add_product方法: class Cart :destroy def add_product(product) current_item = line_items.find_by_product_id(product.id) if current_item current_item.quantity += 1 else current_item = line_items.build(:product_id => product.id, :price => product.price) end current_item end def total_price line_items.to_a.sum { |item| item.total_price } end end 我正在测试将相同的产品添加到购物车两次会增加数量而不是添加新行(line_item)。 这是测试: test “line item quantity is increased when duplicate product is added” do cart = carts(:one) ruby_book = […]

如何在Rails 3中实现批量插入

我需要将一组电子邮件作为不同的记录插入到我的联系人表中。 如何才能做到这一点。 Eg: @email = [“a@b.com”, “c@d.com”, “e@f.com”, … ] 我不想用。 @email.each do |email| @contact = Contact.new @contact.email = email @contact.save end 这导致n插入quires。 我只需要一个插入查询来插入这些值。 如何在rails 3.0.9(以及理想的MySQL)中完成。 请帮忙

如何在Rails应用程序中避免竞争条件?

我有一个非常简单的Rails应用程序,允许用户在一组课程中注册他们的出勤。 ActiveRecord模型如下: class Course < ActiveRecord::Base has_many :scheduled_runs … end class ScheduledRun :attendances … end class Attendance true … end class User :attendances, :source => :scheduled_run end ScheduledRun实例具有有限数量的可用位置,一旦达到限制,就不能再接受更多的考勤。 def full? attendances_count == capacity end attendances_count是一个计数器缓存列,包含为特定ScheduledRun记录创建的出勤关联数。 我的问题是,当一个或多个人同时尝试在课程中注册最后一个可用位置时,我不完全知道确保不会发生竞争条件的正确方法。 我的考勤控制器如下所示: class AttendancesController :create def new @user = User.new end def create unless @user.valid? render :action => ‘new’ end […]