#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我得到哈希但不是我想要的方式.
我试图做类似于stripe的事情.看看他们的数据键.这是一个哈希.
编辑Rich的答案:
从字面上看,我有一个名为UserInterestSent模型和表的模型:
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
我称之为兴趣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
并且兴趣不是利益,专栏:
class AddInterestToUsers < ActiveRecord::Migration def change add_column :users,:text end end
解决方法
This module implements the hstore data type for storing sets of
key/value pairs within a singlePostgresql
value. This can be useful
in varIoUs scenarios,such as rows with many attributes that are
rarely examined,or semi-structured data. Keys and values are simply
text strings.
Heroku supports it我已经看到它用于我正在观察的另一个实时应用程序.
它不会以与Stripe的数据属性相同的方式存储您的对象(为此,您只需要使用文本并保存对象本身),但您可以存储一系列键:值对(JSON).
我以前从未使用它,但我想你可以向列发送一个JSON对象,它将允许你使用你需要的属性.有一个good tutorial here和Rails documentation here:
# 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.interests列中存储信息哈希.也许你有{name:“interest”,type:“sport”}或者什么.
从您的评论中,您似乎希望在此列中存储关联的对象/数据.如果是这种情况,你的方式应该是使用ActiveRecord association.
如果你不知道它是什么,它本质上是一种通过数据库中的外键将两个或多个模型连接在一起的方法.你设置它的方式将决定你可以存储什么&怎么样…
#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,foreign_key: :support_id,association_foreign_key: :user_id end #join table = users_supports (user_id,support_id)
通过使用它,您可以分别填充.interests或.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对象.
好的,看.
我建议的是使用兴趣栏的替代方案.
您似乎想要为关联模型存储一系列哈希.这正是多对多关系的用途.
您的数据被填充两次的原因是因为您正在调用它两次(u =直接在连接模型上创建记录,然后您使用<<)插入更多数据. 我必须在两个实例中都添加,正确的行为正在发生;正在填充连接模型,允许您调用关联的对象. 你想要的是这样的:
def interest_already_sent support = Support.find params[:id] current_user.interests << support end
您可以通过联接表调用.interests.
当使用上面的代码时,它告诉Rails将支持对象(IE support_id插入到current_user(IE user_id)兴趣关联中(用UserInterestSelf表填充).
这将基本上使用current_user的user_id和support的support_id向该表添加新记录.