将Object转换为hash,然后将其保存到用户列
无法找到我正在尝试做的事情。 我想将一个对象存储到用户的列中。 该列采用数组的forms:
#postgres def change add_column :users, :interest, :string, array: true, default: '{}' end
我有另一个名为FooBar的模型用于其他用途。 当我添加了user_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)
我得到哈希值的返回值。 我想当我说u1.interest
我得到同样的东西。 原因是,我需要在用户上定位那些键,即: u1.interest[0].support_id
这可能吗? 我查看了我的基本ruby文档,没有任何作用。 哦..如果我通过FooBar.find(@user).inspect
我得到哈希但不是我想要的方式。
我试图做类似条纹的事情。 看看他们的data
密钥。 这是一个哈希。
编辑Rich的答案:
从字面上看,我有一个名为UserInterestSent
模型和表的模型:
class UserInterestSent < ActiveRecord::Base belongs_to :user belongs_to :support # you can call this post end class CreateUserInterestSents 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
我称之为interest
interest_already_sent
:
supports_controller.rb:
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
并且interest
不是利益 ,专栏:
class AddInterestToUsers < ActiveRecord::Migration def change add_column :users, :interest, :text end end
HStore
我记得有一个名为hStore
的PGSQL数据类型:
此模块实现hstore数据类型,用于在单个
PostgreSQL
值中存储键/值对的集合。 这在各种场景中都很有用,例如具有许多很少检查的属性的行或半结构化数据。 键和值只是文本字符串。
Heroku支持它 ,我已经看到它用于我正在观察的另一个实时应用程序。
它不会以与Stripe
的data
属性相同的方式存储您的对象(为此,您只需要使用text
并保存对象本身),但您可以存储一系列key:value
对(JSON) 。
我以前从未使用它,但我想你可以向列发送一个JSON对象,它将允许你使用你需要的属性。 这里有一个很好的教程 , Rails文档在这里 :
# 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!
–
这意味着您应该只能将JSON对象传递给hstore
列:
#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
根据您的评论,在我看来,您试图将“兴趣”存储在另一个模型的User
。
我的第一个解释是你想在@user.interests
列中存储信息哈希值 。 也许你有{name: "interest", type: "sport"}
或者什么。
从您的评论中,您似乎希望在此列中存储关联的对象/数据。 如果是这种情况,那么您的方式应该是使用ActiveRecord关联 。
如果你不知道它是什么,它本质上是一种通过数据库中的外键将两个或多个模型连接在一起的方法。 你设置它的方式将决定你可以存储什么以及如何…
#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)
通过使用它,您可以分别填充.users
或.users
方法:
#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
这将允许您调用@user.interests
并返回一组Support
对象。
好的,看。
我建议的是使用interest
栏的替代方案 。
您似乎想要为关联模型存储一系列哈希。 这正是many-to-many
关系的用途。
您的数据被填充两次的原因是因为您正在调用它两次( u=
直接在连接模型上创建记录,然后您使用<<
插入更多数据)。
我必须在两个实例中都添加, 正确的行为正在发生; 正在填充连接模型,允许您调用关联的对象。
你想要的是这样的:
def interest_already_sent support = Support.find params[:id] current_user.interests << support end
使用我推荐的方法时,请删除interest
列 。
您可以通过 .interests
表调用.interests
。
当使用上面的代码时,它告诉Rails将support
对象(IE support_id
插入current_user
(IE user_id
) interests
关联(填充UserInterestSelf
表)。
这将基本上使用current_user
的user_id
和support
的support_id
向该表添加新记录。
编辑
要将Hash存储到列中,我建议您使用“text”
def change add_column :users, :interest, :text end
然后将“serialize”设置为属性
class User < ActiveRecord::Base serialize :interest end
一旦完成,您可以正确保存哈希对象
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 = u.attributes # store hash @user.save end
要将AR对象转换为哈希,请使用object.attributes
。
要在模型字段中存储自定义哈希,可以使用serialize或ActiveRecord :: Store
您还可以将to_json方法用作object.to_json
User.find(current_user.id ).to_json # gives a json string