Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将对象转换为散列,然后将其保存到用户';s柱_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 将对象转换为散列,然后将其保存到用户';s柱

Ruby on rails 将对象转换为散列,然后将其保存到用户';s柱,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,找不到任何接近我正在尝试做的事情。我想将一个对象存储到用户的列中。该列的形式为数组: #postgres def change add_column :users, :interest, :string, array: true, default: '{}' end 我有另一个叫FooBar的模型,可供其他用途。由于我添加了一个user\u id键,每个用户的内部都有唯一的信息 我想说得更有道理: def interest @user = User.find(current_user.i

找不到任何接近我正在尝试做的事情。我想将一个对象存储到用户的列中。该列的形式为数组:

#postgres
def change
  add_column :users, :interest, :string, array: true, default: '{}'
end
我有另一个叫FooBar的模型,可供其他用途。由于我添加了一个
user\u id
键,每个用户的内部都有唯一的信息

我想说得更有道理:

def interest
 @user = User.find(current_user.id ) # I need the logged in user's id
 @support = Support.find(params[:id]) # I need the post's id they are on
 u = FooBar.new
 u.user_id = @user
 u.support_id = @support
 u.save # This saves a new Foo object..this is what I want

 @user.interest.push(FooBar.find(@user)) # This just stores the object name itself ;)
end
因此,当我调用
u1=FooBar.find(1)
时,我得到散列中的返回值。我想当我说“兴趣”时,我也会得到同样的结果。原因是,我需要针对用户ie上的那些键:
u1.interest[0]。support\u id

这可能吗?我查看了我的基本ruby文档,但没有任何效果。哦..如果我通过了
FooBar.find(@user.inspect),我会得到散列,但不是我想要的方式

我想做一些类似的事情。查看他们的
数据
键。那是一个杂烩

为富人编辑”回答:

  def interest_already_sent
    support = Support.find(params[:id])
    u = UserInterestSent.new(
      {
        'interest_sent' => 1, # they can only send one per support (post)
        'user_id' => current_user.id, # here I add the current user
        'support_id' => support.id, # and the post id they're on
      }) 
        current_user.interest << u # somewhere this inserts twice with different timestamps
  end 
我有一个名为
UserInterestSent
model and table的模型:

class UserInterestSent < ActiveRecord::Base
  belongs_to :user
  belongs_to :support # you can call this post
end

class CreateUserInterestSents < ActiveRecord::Migration
  def change
    create_table :user_interest_sents do |t|
      t.integer :user_id # user's unique id to associate with post (support)
      t.integer :interest_sent, :default => 0 # this will manually set to 1
      t.integer :support_id, :default => 0 # id of the post they're on

      t.timestamps # I need the time it was sent/requested for each user
    end
  end
end 
以及
兴趣
兴趣,列:

class AddInterestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :interest, :text
  end
end
类AddInterestToUsers
编辑

要将散列存储到列中,我建议您改用“text”

然后将“序列化”设置为属性

class User < ActiveRecord::Base
  serialize :interest
end

要将AR对象转换为哈希,请使用
object.attributes


要在模型字段中存储自定义哈希,您可以使用或

,也可以使用To_json方法作为对象。To_json

User.find(current_user.id ).to_json   # gives a json string
商店 我记得有一个PGSQL数据类型叫做

此模块实现用于存储数据集的hstore数据类型 单个
PostgreSQL
value中的键/值对。这可能很有用 在各种场景中,例如具有许多属性的行 很少检查或半结构化数据。键和值是简单的 文本字符串

我还看到它在我观察的另一个实时应用程序中使用

它不会以与
Stripe
data
属性相同的方式存储对象(为此,您只需要使用
text
并保存对象本身),但您可以存储一系列
键:值
对(JSON)

我以前从未使用过它,但我想您可以向列发送一个JSON对象,它将允许您使用所需的属性。有一个,而且:


根据您的评论,在我看来,您试图将“兴趣”存储在另一个型号的
用户中

我的第一个解释是,您希望在
@user.interests
列中存储散列信息。也许你会有
{name:“interest”,键入:“sport”}
或其他什么

从您的评论来看,您似乎希望在此列中存储关联的对象/数据。如果是这种情况,那么您应该使用

如果您不知道这是什么,它本质上是一种通过数据库中的外键将两个或多个模型连接在一起的方法。您设置它的方式将决定您可以存储什么以及如何

#app/models/user.rb
class User < ActiveRecord::Base
   has_and_belongs_to_many :interests,
      class_name: "Support",
      join_table: :users_supports,
      foreign_key: :user_id,
      association_foreign_key: :support_id
end

#app/models/support.rb
class Support < ActiveRecord::Base
  has_and_belongs_to_many :users,
      class_name: "Support",
      join_table: :users_supports,
      foreign_key: :support_id,
      association_foreign_key: :user_id
end

#join table = users_supports (user_id, support_id)
这将允许您调用
@user.interests
,并带回
支持
对象的集合


好的,看

我建议使用
interest
列作为替代

您似乎希望为关联模型存储一系列哈希。这正是多对多关系的目的。


您的数据被填充两次的原因是您调用了两次(
u=
直接在连接模型上创建了一个记录,然后您使用
插入了更多的数据您是否尝试过在对象上使用
。\u json
并保存它?@martincarlin87是的,但我无法访问任何键ie
u1.interest[0].support_id
@Sylar您能否解释一下为什么不使用常规外键关系(例如,用户模型上的foobar_id字段)?或者,如果您需要保留Foobar的状态,您可以拥有一个名为UserFooBar的子类,该子类存储的数据与存储的数据完全相同,而不进行任何更新。您使用的是哪种SQL?PGSQL允许开箱即用,我认为这是正确的。Postgrest会给我
错误:格式错误的数组文字
是的,我正在手机上查看它。我我不在我的笔记本电脑旁,我会在一小时内尝试。谢谢!当我调用
兴趣
方法时,这会一直覆盖数据。我不想这样。我会进行阅读,因为我确信这很容易完成。谢谢。但你的回答不起作用。我恐怕:(这会覆盖
interest
散列吗?我不想要。我不这么认为。如果它是纯散列,应该设置它。你得到的是重复的散列,因为它们被称为“集合”。如果你需要的话,我可以找到相关信息。是的,我刚刚再次将代码更改为正确的版本。我要去做我的日常工作。晚上我会回来报告。太多的问题都是用“中文”向我报告的。我会按照你展示的图片(约会等)进行,看看效果如何。给我一两天时间。谢谢
User.find(current_user.id ).to_json   # gives a json string
# app/models/profile.rb
class Profile < ActiveRecord::Base
end

Profile.create(settings: { "color" => "blue", "resolution" => "800x600" })

profile = Profile.first
profile.settings # => {"color"=>"blue", "resolution"=>"800x600"}

profile.settings = {"color" => "yellow", "resolution" => "1280x1024"}
profile.save!
#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
   def update 
      @profile = current_user.profile
      @profile.update profile_params
   end

   private

   def profile_params
      params.require(:profile).permit(:x, :y, :z) #-> z = {"color": "blue", "weight": "heavy"}
   end
end
#app/models/user.rb
class User < ActiveRecord::Base
   has_and_belongs_to_many :interests,
      class_name: "Support",
      join_table: :users_supports,
      foreign_key: :user_id,
      association_foreign_key: :support_id
end

#app/models/support.rb
class Support < ActiveRecord::Base
  has_and_belongs_to_many :users,
      class_name: "Support",
      join_table: :users_supports,
      foreign_key: :support_id,
      association_foreign_key: :user_id
end

#join table = users_supports (user_id, support_id)
#config/routes.rb
resources :supports do
   post :interest #-> url.com/supports/:support_id/interest
end

#app/controllers/supports_controller.rb
class SupportsController < ApplicationController
   def interest
       @support = Support.find params[:support_id] # I need the post's id they are on
       current_user.interests << @support
   end
end
def interest_already_sent
    support = Support.find params[:id]
    current_user.interests << support
end