stephencelis / acts_as_singleton Goto Github PK
View Code? Open in Web Editor NEWA lightweight singleton library for your Active Record models.
A lightweight singleton library for your Active Record models.
the instance
method should be
def instance
self.transaction { first || create }
end
thereby preventing more than one record from being create
'ed
Trying to make a form with a singleton model using form_for
doesn't really work. It'd be nice if it did work.
The code below is running perfectly well with rails 2.3.5 but not with rails 3.0.0.4beta:
class FeaturedMember < ActiveRecord::Base
acts_as_singleton
belongs_to :user
end
( $ ) rails c
Loading development environment (Rails 3.0.0.beta4)
irb(main):001:0> FeaturedMember.instance
NoMethodError: private method find_by_sql' called for #<Class:0xb5bde13c> from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1016:in
method_missing' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation.rb:64:in to_a' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:324:in
find_first' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:117:in first' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:403:in
send' from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:403:in first' from /usr/lib/ruby/gems/1.8/gems/acts_as_singleton-0.0.4/lib/acts_as_singleton.rb:43:in
instance' from (irb):1
irb(main):002:0>
Hi,
I have the following setup:
class VirtualPlaylist < ActiveRecord::Base; acts_as_singleton; end
class MyFavorites < VirtualPlaylist; end
class RecentlyPlayedSongs < VirtualPlaylist; end
Accessing the singletons fails in this case with the following error:
musicsocial> VirtualPlaylist.instance
=> #<VirtualPlaylist name: nil, type: nil>
musicsocial> MyFavorites.instance
=> #<MyFavorites name: nil, type: "MyFavorites">
musicsocial> MyFavorites.instance
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'MyFavorites'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite MyFavorites.inheritance_column to use another column for that information.
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:1637:in `instantiate_without_polymorphic_checks'
from /Users/karl/projects/musicsocial/vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/base.rb:52:in `instantiate'
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:661:in `find_by_sql'
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:661:in `collect!'
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:661:in `find_by_sql'
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:1548:in `find_every'
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:1505:in `find_initial'
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:613:in `find'
from /Users/karl/projects/musicsocial/vendor/rails/activerecord/lib/active_record/base.rb:623:in `first'
from /Users/karl/projects/musicsocial/vendor/plugins/acts_as_singleton/lib/acts_as_singleton.rb:42:in `instance'
from (irb):4
However if I comment out the line in acts_as_singleton.rb which privatizes a bunch of the instance methods, it works.
#private_class_method *methods.grep(PRIVATE) # Deny existent others.
musicsocial> VirtualPlaylist.instance
=> #<VirtualPlaylist name: nil, type: nil>
musicsocial> MyFavorites.instance
=> #<MyFavorites name: nil, type: "MyFavorites">
musicsocial> RecentlyPlayedSongs.instance
=> #<RecentlyPlayedSongs name: nil, type: "RecentlyPlayedSongs">
I'm not sure which method it is that is causing the problem.
Regards,
Karl
...and bundle update rails fails with:
bundle update rails
Fetching source index for http://rubygems.org/
Bundler could not find compatible versions for gem "activesupport":
In Gemfile:
railties (~> 3.1.0.rc1) depends on
activesupport (= 3.1.0.rc1)
acts_as_singleton (= 0.0.6.beta) depends on
activesupport (3.1.0.rc4)
ActiveRecord in Rails 3 needs to be able to call find_by_sql
in order for acts_as_singleton
to work. See this commit for a patch.
When invoking ::instance
ArgumentError: wrong number of arguments (2 for 1)
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:170:in find_by_sql' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:170:in
exec_queries'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:159:in to_a' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/explain.rb:40:in
logging_query_plan'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation.rb:158:in to_a' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:377:in
find_first'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:122:in first' from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/querying.rb:5:in
send'
from /.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.1/lib/active_record/querying.rb:5:in first' from /.rvm/gems/ruby-1.8.7-p357/gems/acts_as_singleton-0.0.5/lib/acts_as_singleton.rb:43:in
instance'
Given this simple model
class Platform < ActiveRecord::Base
acts_as_singleton
end
and using 3.1.0.rc4 I get the following error when doing a
Platform.instance
in the rails console:
>> Platform.instance
ArgumentError: wrong number of arguments (2 for 1)
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/bundler/gems/acts_as_singleton-4229bec5929a/lib/acts_as_singleton.rb:58:in `find_by_sql'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:109:in `to_a'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:370:in `find_first'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:122:in `first'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:444:in `first'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/bundler/gems/acts_as_singleton-4229bec5929a/lib/acts_as_singleton.rb:43:in `instance'
from (irb):1
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
from /home/wtf/.rvm/gems/ruby-1.9.2-p180@satellite/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>
Even when using the most recent version via adding this line
gem 'acts_as_singleton', :git => 'https://github.com/stephencelis/acts_as_singleton.git'
to my Gemfile.
It seems to be the same issue as here: #3
Any ideas?
Given
class God
acts_as_singleton
has_many :children, :as => :parent
end
class Child
belongs_to :parent, :polymorphic => true
validates_associated :parent
end
Child create seems to fail because the validation is calling 'find' on God which is now a private acts_as_singlton method.
Rails 4.1.10 & acts_as_singleton 0.0.8
I'm getting the following error when calling an instance:
rails c test
Loading test environment (Rails 4.1.10)
irb: warn: can't alias context from irb_context.
2.1.5 :001 > GlobalSetting.instance
GlobalSetting Load (2.8ms) SELECT "global_settings".* FROM "global_settings" ORDER BY "global_settings"."id" ASC LIMIT 1
(1.1ms) BEGIN
(0.3ms) ROLLBACK
NoMethodError: private method `find_generated_attribute_method' called for #<Class:0x007fc79903bea8>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.