Tag: activerecord

Rails让ActiveRecord一次性获取多个关联?

下面的问题有一个很好的答案,可以使用Comment.includes(:user)在一次点击中获取activerecord集合的关联值。 当你想要一次性获取多个关联时怎么办? Rails有活跃记录一次性获取所有需要的关联吗? 将这些链接在一起的最好方法就像Customer.includes(:user).includes(:sales).includes(:prices)或者是否有更清洁的方式。 此外,当我在索引表上的循环上执行此操作时。 我可以在customer.rb模型上添加一个方法,以便我可以调用@customers.table_includes等 def table_includes self.includes(:user).includes(:sales).includes(:prices) end 为了记录,我测试了上面的内容并且它没有用,因为它是一个集合上的方法(还没有弄清楚如何做到这一点)。

在进行ActiveRecord :: Base计数时如何按计数desc进行排序?

一个例子是…… Pets.find(:all, :select => ‘count(*) count, pet_type’, :group => ‘pet_type’, :order => ‘count’) 返回正确的结果,但不返回返回的orderedhash对象中的实际计数。 Pets.count(:all, :group => ‘pet_type’) 返回计数但不按降序排序……我该怎么做? 我想我更喜欢使用.find ..但如果我可以对它进行排序,我会接受.count。

ActiveRecord – 查询所有并包含符合条件的关联

环境 :Rails 3.2.22 问题 : 让我们说我有模型Topics , Posts和User 。 Posts属于Topics User有很多Posts 我想查询Topic.all ,但包括与用户关联的所有post。 我已尝试使用include和eager_load以及用户标识的where条件,但只返回满足条件的post。 我想要的是所有主题返回并仅包含与user_id条件匹配的post。

ActiveRecord加入聚合查询

我有一个Unit模型has_many :transacions 。 交易有一个type列,表明它是什么类型的交易:销售,转让,预订等。 我想找到具有给定交易类型的所有单位作为他们的最新交易。 我无法弄清楚如何在ActiveRecord中执行此操作。 在SQL中,如果我想找到所有以“Transfer”作为最新事务的单位,我可以这样做: SELECT u.*, tx.* FROM units u INNER JOIN transactions tx ON u.id=tx.unit_id INNER JOIN ( SELECT tx.unit_id, max(tx.created_at) AS latest_tx_date FROM transactions tx GROUP BY tx.unit_id ORDER BY tx.unit_id ) max_dates ON (tx.unit_id=max_dates.unit_id AND tx.created_at>=max_dates.latest_tx_date) WHERE tx.type=’Transfer’; 这是我遇到麻烦的内部查询的加入。 是否有可读的ActiveRecord方法来执行此操作,还是应该使用SQL?

Rails:如何获取深度为2’has_many’级别的记录?

我已经设置了这些模型: Course has_and_belongs_to_many Student Student has_and_belongs_to_many Course has_many Books Book belongs_to Student 如何使用ActiveRecord高效获取课程的所有书籍?

如何检查一个数字是否在activerecord范围内?

我有一个带范围列的模型 # 如何使用age: 20来获得JobRequirement ? 就像是 JobRequirement.where(age: 20)

Ruby on Rails查询无法正常工作

我有几个列表,我定义了一些filter。 特别是,列出has_many :spaces, through: :designations has_many :amenities, through: :offerings has_many :spaces, through: :designations和has_many :amenities, through: :offerings 。 我使用filter来限制显示的列表。 两个主要的是: # filter by space type if params[:search][:space_ids].present? && params[:search][:space_ids].reject(&:blank?).size > 0 @listings = @listings.joins(:spaces).where(‘space_id IN (?)’, params[:search][:space_ids].reject(&:blank?)).uniq end # filter by amenities if params[:search][:amenity_ids].present? && params[:search][:amenity_ids].reject(&:blank?).size > 0 @listings = @listings.joins(:amenities).where(amenities: { id: params[:search][:amenity_ids].reject(&:blank?) }).group(‘listings.id’).having(‘count(*) = […]

在rails关系数据库中创建和实施has_one关系

在查看的配置文件表上工作。 我有一个问题设置,这是正确的吗? 我对has_many和has_one关系感到困惑。 因为这是一个每个访问过的关系都有一行的表,所以我决定使用has_one。 这看起来是否正确,是否有办法在ActiveRecord中强制执行关系? 模型 class ViewedProfile < ActiveRecord::Base validates :viewed_profile_id, presence: true validates :profile_id, presence: true has_one :profile_id has_one :viewed_profile_id end 移民 class CreateViewedProfile < ActiveRecord::Migration def change create_table :viewed_profiles do |t| t.integer :profile_id t.integer :viewed_profile_id end end end 编辑 此外,当我进入我的控制台并输入ViewedProfile时,没有任何内容出现。 任何想法为什么? = c架构通常应该出现!

Ruby on Rails 3 – 序列化数组不匹配的类障碍

嘿那里,我对Rails 3.0.5和Ruby 1.9.2中的序列化不匹配问题感到困惑。 我使用Array的子​​类为数据库播种,然后尝试保存到ActiveRecord对象。 谁能帮帮我吗? 我最初尝试序列化为Graph,但将其缩减为Array以避免自定义类的错误。 我很难过,因为这对我来说没有直觉意义。 非常感谢您的帮助! class Graph < Array .. class Settings < ActiveRecord::Base serialize :graphs, Array .. 在seeds.rb中最后一行 – 例如之后没有任何事情发生。 为了调试目的,只需节省很多: sh_1g = sh_1g.to_a d.company.settings.add_graph(sh_1g.to_a) d.company.settings.save! d.company.save! d.save! if sh_1g == d.company.settings.graphs[0] puts “the added graph matches the first graph in the graphs array” end puts “added ” + sh_1g.inspect + […]

如何更新接受嵌套属性的深层嵌套模型?

我有一个问题大多与此相同。 我有三个模特称他们为父,子,孙。 Parent has_many :children has_many :grandchildren, through: children 创建父项时,我通过集合复选框为其分配子项。 现在我需要更新父表单上的孙子关联。 我成立了 accepts_nested_attributes_for :children 我能够像我需要的那样更新属性。 当我尝试更新父记录时出现问题。 如果我删除一个孩子(通过取消它的复选框)我最终得到错误 ActiveRecord::RecordNotFound Couldn’t find Child with ID=# for Parent with ID=# 即使这种关联在数据库中定义得很好。 经过进一步调查,我发现错误来自 activerecord (4.1.1) lib/active_record/nested_attributes.rb:545:in `raise_nested_attributes_record_not_found!’ 有谁知道如何解决这个问题? Ruby 1.9.3,Rails 4.1.1 在我的控制器中我有 params.require(:Parent).permit(:name, child_attributes: [:id, :grandchild_id], :child_ids => [])