Closed gleuch closed 11 years ago
Hello @gleuch, thanks for the feedback.
Sorry but I didn't quite get it. Do you use RedisOrm along with ActiveRecord? Or you pointing out the discrepancy in APIs between them?
Also I couldn't understand why the error "Couldn't find User without an ID" would be shown after invoking User.first command? Usually it'll return just nil if there are no Users at all (I just rerun the code you provided and it works fine (if user assoc isn't necessary for Comment model)).
Patch would be ok, but providing the failing test is even better. Thanks!
The goal was to make a has_many associated with an ActiveRecord model, which I was unable to do. After trying to debug this issue, I realized that ActiveRecord too would also need patching to get a belongs_to association from it to work with a Redis_Orm model. Defeats my purpose for mixed-model associations.
This trouble was w/ Rails 3.2.8.
The issue is not User.first. The issue is when saving a new Redis_Orm record that had an association with an ActiveRecord model, it would fail. Looking at the code, it seems that a check to find the associated record happens whether or not there is a previously associated id, in which saving a new record should not need to do this lookup. The old_assoc lookup should return nil if it is a new record or has not been assigned before, rather than blindly trying it. (The issue arises when doing a find on ActiveRecord model where the id is nil, as ActiveRecord raises error if id is nil.)
On Wed, Sep 12, 2012 at 6:13 PM, Dmitrii Samoilov notifications@github.comwrote:
Hello @gleuch https://github.com/gleuch, thanks for the feedback.
Sorry but I don't quite get it. Do you use RedisOrm along with ActiveRecord? Or pointing out the discrepancy in APIs between them?
Also I couldn't understand why the error "Couldn't find User without an ID" would be shown after invoking User.first command? Usually it'll return just nil if there are no Users at all (I just rerun the code you provided and it works fine (if user assoc isn't necessary for Comment model)).
Patch would be ok, but providing the failing test is even better. Thanks!
— Reply to this email directly or view it on GitHubhttps://github.com/german/redis_orm/issues/2#issuecomment-8512023.
The goal was to make a has_many associated with an ActiveRecord model, which I was unable to do.
Yes, there's no possibility to do that in current version of RedisOrm. There are certain constrains like backlinks (like you've mentioned earlier), so I don't think that's possible. However you could make your own fork and experiment with it.
Anyway why should one mix and match RedisOrm and ActiveRecord? I mean either you do the whole project on Redis or you do it on some relational database and use Redis as a storage for sessions or cached views.
When building a
has_one
orbelongs_to
association that maps to an ActiveRecord model, a lookup is done in each of the association types for an old association that will later be cleared. However, when the lookup happens when there is no prior association, it fails. As such:... would fail on line 2 because of the way the lookup is done for a has_one and belongs_to lookup with:
The lookup is made in
lib/redis_orm/associations/belongs_to.rb
, on lines 43-44,... that calls
foreign_model_name
method that finds the record using the id value it gets from Redis, which returns nil if never previously associated. When nil is passed to an ActiveRecord model'sfind
method, ActiveRecord raises an error becausefind_by_ids
checks to see if there are 0, 1, or many ids being passed, and raising an error if the length is 0. (See: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rb#L244)When
old_assoc
is later checked to see if the old associated records (if !old_assoc.nil?
/if old_assoc
), the best fix, IMO, would be to addrescue nil
when sendingforeign_model_name
, or adding a rescue clause inforeign_model_name
, such as:Another option is to adding some check during
foreign_model_name
to see if an association exist before finding the associated record, return nil before attempting to find the associated record.Holla which method you like and I can help patch.