简体   繁体   English

Rails 4 / Postgresql 9.4-查询和更新数组中的ruby对象

[英]Rails 4 / postgresql 9.4 - Query and update a ruby object inside an array

I have a method (find_by_sql) that outputs a ruby object inside an array like this: 我有一个方法(find_by_sql),它在数组中输出一个ruby对象,如下所示:

[#<deal id: 66480, opportunity_id: 4, admin_user_id: 1, created_at: "2015-09-20 18:37:29", updated_at: "2015-09-20 18:37:29", deal_available: true>]

How can I update the object's 'deal_available' attribute inside my database in raw postgresql ? 如何在原始postgresql中更新数据库中对象的'deal_available'属性?

I tried different ways to write it but I stumble on the fact that it's very specific: it's an array, and inside there is a ruby object and I must manage to tell postgresql to change the value of deal_available: true to deal_available: false. 我尝试了不同的方式编写它,但是我偶然发现了它非常具体的事实:它是一个数组,并且内部有一个ruby对象,我必须设法告诉postgresql将deal_available:true的值更改为deal_available:false。

In local, I tried: logger.debug "Here is the resulting object for update: 在本地,我尝试过:logger.debug“这是要更新的结果对象:

 #{@deal_question[0]}

but I get: 但我得到:

#<@deal_question:0x007fd9b3963270>

Here is how @deal_question is created 这是创建@deal_question的方法

@deal_question = Deal.find_by_sql(
      " SELECT \"deals\".*
        FROM \"deals\"
        WHERE (opportunity_id = #{@deal.id}
        AND deal_available = true)
        ORDER BY \"deals\".\"id\" ASC LIMIT 1"
    ) 

I don't even have a postgresql query to suggest here because first I need to understand how to query this ruby object inside the array. 我什至没有在此建议的postgresql查询,因为首先我需要了解如何在数组内部查询此ruby对象。

You actually don't need a raw SQL query to update the deal_available attribute of your @deal_question object. 实际上,您实际上不需要原始SQL查询来更新@deal_question对象的deal_available属性。

You can use update_attributes to achieve that. 您可以使用update_attributes来实现。

# assuming @deal_question is an array
@deal_question[0].update_attributes(deal_available: false)

If you really want to use raw sql to update the attribute, then do this way: 如果您确实要使用原始sql更新属性,请执行以下方法:

array = [#<deal id: 66480, opportunity_id: 4, admin_user_id: 1, created_at: "2015-09-20 18:37:29", updated_at: "2015-09-20 18:37:29", deal_available: true>]
# grab the object to be updated
@deal_question = array[0]
# build the sql query string
sql = "UPDATE deals SET deal_available = false WHERE opportunity_id = #{@deal_question.id}"
# execute the sql to update the object
ActiveRecord::Base.connection.execute(sql)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Active Record查询json数组(Rails 4 / postgresql9.4) - Query an array of json with Active Record (Rails 4/postgresql9.4) 将ActiveRecord语句转换为Postgresql查询-Rails 4 / postgresql 9.4 - Convert ActiveRecord statement into postgresql query- Rails 4/postgresql 9.4 查询Ruby on Rails和PostgreSQL上的数组类型字段 - Query for array type field on Ruby on Rails and PostgreSQL 如何在Rails上使用PostgreSQL和Ruby更新数组类型的列 - How to update a column of type array with postgresql and ruby on rails 如何在Ruby on Rails中使用PostgreSQL JSON数组查询ActiveRecord :: Relation - How to use the PostgreSQL JSON array query ActiveRecord::Relation in Ruby on Rails 如何在 Rails 6 上的 Ruby 上正确查询 Postgresql JSONB 哈希数组? - How to properly query Postgresql JSONB array of hashes on Ruby on Rails 6? Rails / PostgreSQL:整数数组列中的查询值 - Rails/PostgreSQL : query value inside an integer array column 更新has_many / belong_to关系中大量关联对象的行(Rails 4,PostgreSQL 9.4,activeadmin) - Update massive number of rows of associated objects in has_many/belong_to relations (Rails 4, postgresql 9.4, activeadmin) ruby on rails postgresql 活动记录并行更新 - ruby on rails postgresql active record parallle update 如何在 ruby​​ on rails 中查询 Postgresql HSTORE - How to query Postgresql HSTORE in ruby on rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM