Comments (8)
Sorry, I've not been well -- I haven't tested this with ActiveModel past v4, so something may have changed in that. I'll fix this when I get some time.
from zermelo.
Digging more, it doesn't appear that the Redis
module exists: https://github.com/flapjack/zermelo/blob/master/lib/zermelo/records/redis.rb#L7-L27
Any chance you could share more insight regarding when to pick the RedisSet or RedisSortedSet?
Thanks in advance
from zermelo.
Using the Post
class example and fixing the include
:
class Post
include Zermelo::Records::RedisSet
define_attributes :title => :string,
:score => :integer,
:timestamp => :timestamp,
:published => :boolean
end
It appears that the id is not automatically generated:
[20] pry(main)> post = Post.new(:title => 'Introduction to Zermelo',
[20] pry(main)* :score => 100, :timestamp => Time.parse('Jan 1 2000'), :published => false)
=> #<Post:0x00007f831ca0f608
@attributes=
{"id"=>nil,
"title"=>"Introduction to Zermelo",
"score"=>100,
"timestamp"=>2000-01-01 00:00:00 -0800,
"published"=>false},
@is_new=true>
Which leads to calling changed?
method on nil
:
[21] pry(main)> post.save
NoMethodError: undefined method `changed?' for nil:NilClass
from /usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.2.1/lib/active_model/attribute_mutation_tracker.rb:49:in `changed?'
I'm not sure at this point if I'm supposed to keep track of the ids? Seems counter-intuitive but let me know.
I tried to pass an id by hand as well:
[23] pry(main)> p = Product.new(vendor: 'apple', name: 'news', id: 42)
=> #<Product:0x00007f831c991a00
@attributes={"id"=>42, "name"=>"news", "vendor"=>"apple"},
@is_new=true>
[24] pry(main)> p.save
=> false
[25] pry(main)> p.save
=> false
[26] pry(main)> p.errors
=> #<ActiveModel::Errors:0x00007f831f42c468
@base=
#<Product:0x00007f831c991a00
@attributes={"id"=>42, "name"=>"news", "vendor"=>"apple"},
@errors=#<ActiveModel::Errors:0x00007f831f42c468 ...>,
@is_new=true,
@validation_context=nil>,
@details={:id=>[{:error=>"should be String but is Integer"}]},
@messages={:id=>["should be String but is Integer"]}>
[27] pry(main)> p = Product.new(vendor: 'apple', name: 'news', id: '42')
=> #<Product:0x00007f831d0e9640
@attributes={"id"=>"42", "name"=>"news", "vendor"=>"apple"},
@is_new=true>
[28] pry(main)> p.save
NoMethodError: undefined method `multi' for nil:NilClass
from /usr/local/lib/ruby/gems/2.5.0/bundler/gems/zermelo-d7adfdd0b29a/lib/zermelo/backends/redis.rb:120:in `begin_transaction'
from zermelo.
It's looking like you've not initialised Zermelo.redis
to a valid Redis connection? ( https://github.com/flapjack/zermelo#initialisation )
For the inclusion of the gem, in this case
require 'zermelo'
require 'zermelo/records/redis'
should be enough in terms of the requires, and the first of those would be implicit in the Gemfile, yeah.
Ids are left up to the implementing classes, at the moment. It's a design decision based on what I needed it for (an app that wanted that level of control), but I did have plans to make that more pluggable and provide some standard implementations.
from zermelo.
Also, for a Set or SortedSet, it really depends on how you'd want to query that data. Is set inclusion all you care about, or is the order (and possible range querying) desirable?
from zermelo.
It's looking like you've not initialised
Zermelo.redis
to a valid Redis connection? ( flapjack/zermelo#initialisation )
Thanks for the quick answer!
Indeed after requiring the redis gem, I got a little further but unfortunately a different error popped up:
[31] pry(main)> require 'redis'
=> true
[32] pry(main)> Zermelo.redis = Redis.new(host: '127.0.0.1', db: 0)
=> #<Redis client v4.0.3 for redis://127.0.0.1:6379/0>
[33] pry(main)> p
=> #<Product:0x00007f831d0e9640
@attributes={"id"=>"42", "name"=>"news", "vendor"=>"apple"},
@errors=
#<ActiveModel::Errors:0x00007f831d08a9d8
@base=#<Product:0x00007f831d0e9640 ...>,
@details={},
@messages={}>,
@is_new=true,
@validation_context=nil>
[34] pry(main)> p.save
NoMethodError: undefined method `forgetting_assignment' for ["id", "42"]:Array
from /usr/local/lib/ruby/gems/2.5.0/gems/activemodel-5.2.1/lib/active_model/dirty.rb:270:in `each'
For what it's worth, when trying to run p.save
, redis-cli monitor
returns the following:
1542845567.443789 [0 127.0.0.1:62114] "multi"
1542845567.444801 [0 127.0.0.1:62114] "sadd" "product::attrs:ids" "42"
1542845567.444820 [0 127.0.0.1:62114] "hmset" "product:42:attrs" "name" "news" "vendor" "apple"
1542845567.444844 [0 127.0.0.1:62114] "exec"
I'm not too sure about the forgetting_assignment
if I'm missing something in the model.
Thanks again for the help, much much appreciated!
from zermelo.
@ali-graham no worries please, I just wanted to give some feedback and making sure I was not missing anything.
Thanks for sharing your wok with this gem, it's much appreciated and I hope you feel better soon
from zermelo.
I had some travel too, so my time's been a bit broken up. There have been some extensive changes in recent versions of ActiveModel, I'll iron out the remaining bugs in the branch when I get a chance (it seems like my code can continue doing what it was doing, it just needs to use new wrapper objects etc.), and then work out the proper points to conditionally use those in master
in order to support both older and recent versions of AM.
from zermelo.
Related Issues (19)
- Update sorted_set scores when key value changes HOT 1
- Clean up Redis sorting code (if redis server is recent enough)
- Determine lowest Redis version supported, ensure this is checked for each new connection
- Add save! method, raise an exception on failure.
- Cache Redis query result fragments using predictable names
- InfluxDB support HOT 3
- Optional separate zset indexing, value -> id (useful for timestamps etc.)
- .clear / .delete_ids methods for multiple associations
- get travis building with some earlier redis versions
- Class- and instance-level methods to emit all used Redis key names HOT 1
- association add/remove changes aren't saved atomically HOT 1
- Sentinel support HOT 4
- Statistics calculation on record save
- Optionally output all database operations to logger
- index by data type *and* string representation HOT 1
- sandstorm gem name is being squatted HOT 1
- Association callbacks
- allow filter queries to use association id values HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zermelo.