soveran / ohm

Object-Hash Mapping for Redis

Home Page:http://ohm.keyvalue.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ohm::Model relationship with ActiveRecord::Base`?

umdstu opened this issue · comments

I saw #181 mention this, I think, but the link to his code is dead now. I have a few models which persist to MySQL, and need to stick around. Then there is a model I have which are generated by the thousands and deleted by the thousands throughout the day, and mysql is currently a bottleneck for me.

Before seeing #181 I wrote this in my Ohm::Model based on what the reference shortcut was really doing.

class GalModelResult < Ohm::Model
  attribute :vote, :gal_model_id, :vote, :score
  index :gal_model_id

  def gal_model=(gal_model)
    self.gal_model_id = gal_model.id
  end

  def gal_model
    GalModel.find_by_id(gal_model_id)
  end 
end

class GalModel < ActiveRecord::Base
     has_many :gal_model_results
end

But i'm guessing it won't work based on #181. Is this the case? Is there another way to do this? Non-ohm recommendations?

Hello @umdstu, I think it may work for your use case. The issue in #181 has to do with using instances of Ohm::Model with helpers that are designed to deal with instances of ActiveModel. There are probably some way to solve that with a wrapper, but if you don't have the need to use your models that way, then you should be good to go. Check this (very old) article about how to mix the two.

Please reopen this issue if the code I mentioned doesn't solve the problem.