2015-10-13 36 views
4

找不到任何接近我想要做的事情。我想將一個對象存儲到用戶的列中。該列是處於陣列的形式:將一個對象轉換爲散列,然後將其保存到用戶列

#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。看看他們的data鍵。這是一個散列。

編輯豐富的回答:

我已,從字面上看,一個模式叫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 

我打電話interestinterest_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 
+0

你試圖使用'.to_json'的對象和保存的? – martincarlin87

+0

@ martincarlin87是的,但我不能讓任何按鍵,即'u1.interest [0 ] .support_i d' – Sylar

+0

@Sylar你能否解釋爲什麼你不使用常規的外鍵關係(例如, foob​​ar_id作爲用戶模型中的字段)?或者,如果你需要保留Foobar的狀態,你可以有一個名爲UserFooBar的子類,它存儲的數據與沒有更新時一樣。 –

回答

1

HStore

我記得有一個名爲hStore一個PGSQL數據類型:

This module implements the hstore data type for storing sets of key/value pairs within a single PostgreSQL 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,我已經看到了它在其他實時應用,我觀察使用。

它不會以同樣的方式你的對象存儲爲Stripedata屬性(即,你只需要使用text並保存對象本身),但你可以一系列key:value對(JSON)。

我從來沒有用過它,但我想象你可以發送一個JSON對象到列中,它將允許你使用你需要的屬性。有一個good tutorial hereRails 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中存儲「興趣」。

我的第一個解釋是,您想在@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, 
     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) 

利用這一點,你可以分別填充.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對象。


好的,看。

我建議的是替代使用interest列。

您似乎想要爲相關模型存儲一系列散列。這正是many-to-many關係的目的。

你的數據被填充兩次的原因是因爲你調用它兩次(u=直接的連接模型創造了記錄,然後你用<<插入更多的數據)。

我必須補充說明,在兩個實例中,正確的行爲正在發生;正在填充連接模型,允許您調用關聯的對象。

你要的是什麼是這樣的:

enter image description here

def interest_already_sent 
    support = Support.find params[:id] 
    current_user.interests << support 
end 

當使用我推薦的方法,擺脫interest的。

您可以撥打.interests通過連接表

當使用上面的代碼,它告訴導軌插入support對象(IE support_idcurrent_user(IE user_idinterests關聯(填充了UserInterestSelf表)。

這將基本上然後添加新記錄該表的current_useruser_idsupportsupport_id

+0

這會覆蓋'interest'散列嗎?我不想這樣。 – Sylar

+0

我不這麼認爲。如果是純散列,應該設置 –

+0

好吧。第一個答案覆蓋整個散列我現在試着你的答案,然後讓你知道 – Sylar

1

編輯

要存儲散列成列,我建議你使用 「文本」,而不是

def change 
    add_column :users, :interest, :text 
end 

然後設置「序列化」爲屬性

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 
+0

,給了我'錯誤:格式不正確' – Sylar

+0

我更新了我的答案 – byakugie

+0

是的,我正在從我的手機看它。我不在我的筆記本電腦上,我會在一小時內嘗試。謝謝! – Sylar

-1

要轉換AR對象散列使用object.attributes

要儲存一個自定義哈希在模型字段可以使用serializeActiveRecord::Store

+0

我會讀一讀,因爲它確實可以輕鬆完成。謝謝。但你的答案沒有工作,我害怕:( – Sylar

-1

您還可以使用to_json方法對象。to_json

User.find(current_user.id).to_json # gives a json string 
相關問題