Convert the object to a hash, then save it in the user column

Could not find anything close to what I'm trying to do. I want to save an object in a user column. This column is in the form of an array:

#postgres
def change
  add_column :users, :interest, :string, array: true, default: '{}'
end

I have another model called FooBar for another use. Each user has unique information inside as I added a key user_id.

I am trying to make more sense:

def interest
 @user = User.find(current_user.id ) # I need the logged in user id
 @support = Support.find(params[:id]) # I need the post 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

So when I call u1 = FooBar.find(1), I return the value to the hash. I want when I say u1.interest, I get the same. The reason is because I need to target these user keys:u1.interest[0].support_id

Is it possible? I looked through my main ruby ​​documents and nothing works. Oh, if I pass FooBar.find(@user).inspect, I get a hash, but not the way I want.

- stripe. data. .

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

HStore

PGSQL hStore:

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 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 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 .

Rails Support (IE support_id current_user (IE user_id) interests ( UserInterestSelf).

user_id current_user support_id Support.

+1

, ""

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 id
  @support = Support.find(params[:id]) # I need the post 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
+1

AR , object.attributes.

, serialize ActiveRecord::

-1

to_json object.to_json

User.find(current_user.id ).to_json   # gives a json string
-1

Source: https://habr.com/ru/post/1611394/


All Articles