I want to show first users with last_request >= 1.day.ago and then add the rest users
def self.default_scope
where("last_request >= ?", 1.day.ago) + where("last_request < ? OR last_request is null", 1.day.ago)
end
This code raises that error:
undefined method `merge' for []:Array
How could I do that?
UPDATE
Error stack
NoMethodError - undefined method `merge' for #<Array:0xd16ba90>:
activerecord (3.2.13) lib/active_record/relation.rb:503:in `with_default_scope'
activerecord (3.2.13) lib/active_record/relation.rb:167:in `exec_queries'
activerecord (3.2.13) lib/active_record/relation.rb:160:in `block in to_a'
activerecord (3.2.13) lib/active_record/explain.rb:34:in `logging_query_plan'
activerecord (3.2.13) lib/active_record/relation.rb:159:in `to_a'
will_paginate (3.0.5) lib/will_paginate/active_record.rb:127:in `block in to_a'
will_paginate (3.0.5) lib/will_paginate/collection.rb:96:in `create'
will_paginate (3.0.5) lib/will_paginate/active_record.rb:126:in `to_a'
UPDATE2
I am using rails_admin, datetime field ordering is not working correctly
1)Order by ascending
2)Order by descending
In your particular case you can combine scopes to one with
default_scope where('last_request >= :time OR (last_request < :time OR last_request IS NULL)', time: 1.day.ago).order('last_request DESC')
UPD
That is not ActiveRecord or RailsAdmin issue, more info
ORDER BY ASC with Nulls at the Bottom
Rails: Order with nulls last
Related
I'm using Rails 5.0.5 with Devise 4.3.0 for authentication. This app has been running smoothly for months, until I added a 'type'=>'string' attribute to my User model and attempted to create a new user. Submitting the form gives me a 500 internal server error. In this example, the User.type = 'hunter'.
NameError - wrong constant name hunter:
activesupport (5.0.5) lib/active_support/inflector/methods.rb:268:in `const_get'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:268:in `block in constantize'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:266:in `each'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:266:in `inject'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:266:in `constantize'
activesupport (5.0.5) lib/active_support/dependencies.rb:583:in `get'
activesupport (5.0.5) lib/active_support/dependencies.rb:614:in `constantize'
activerecord (5.0.5) lib/active_record/inheritance.rb:177:in `find_sti_class'
activerecord (5.0.5) lib/active_record/inheritance.rb:209:in `subclass_from_attributes'
activerecord (5.0.5) lib/active_record/inheritance.rb:55:in `new'
devise (4.3.0) lib/devise/models/registerable.rb:20:in `new_with_session'
app/models/user.rb:58:in `new_with_session'
user.rb:
def self.new_with_session(params, session)
if session['devise.user_attributes']
new(session['devise.user_attributes']) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
Is Rails think this attribute value is a ClassName?? Can't seem to figure this one out. Any help greatly appreciated.
ActiveRecord uses the type column for Single Table Inheritance (STI) by default and the type value is expected to name a class. Presumably you don't have a hunter class so you get a confusing NameError from deep inside the guts of ActiveRecord.
From the fine manual:
inheritance_column()
Defines the name of the table column which will store the class name on single-table inheritance situations.
The default inheritance column name is type, which means it's a reserved word inside Active Record. To be able to use single-table inheritance with another column name, or to use the column type in your own model for something else, you can set inheritance_column:
self.inheritance_column = 'zoink'
Either rename your type column to something else or tell ActiveRecord to use some other column name for STI:
class User < ApplicationRecord
self.inheritance_column = 'there_is_no_sti_here' # Or whatever you're not using for a column name.
end
Using self.inheritance_column = nil also works.
If you're doing this a lot then you could make it declarative by adding a concern:
module STISuppression
extend ActiveSupport::Concern
included do
self.inheritance_column = nil
end
end
and then say things like:
class SomeModel < ApplicationRecord
include STISuppression
end
Same effect but it makes it clear what you're up to.
I am trying to do pagination (will_paginate gem) on my project model that has many comments (acts_as_commentable_with_threading gem):
#root_comments = #project.root_comments.paginate(:per_page => 2, :page => params[:discussions_page])
Stack Trace:
NoMethodError - undefined method `total_pages' for #<Comment::ActiveRecord_AssociationRelation:0x007fa183d77ce8>:
activerecord (4.1.4) lib/active_record/relation/delegation.rb:136:in `method_missing'
activerecord (4.1.4) lib/active_record/relation/delegation.rb:99:in `method_missing'
will_paginate (3.0.5) lib/will_paginate/view_helpers.rb:73:in `will_paginate'
will_paginate (3.0.5) lib/will_paginate/view_helpers/action_view.rb:33:in `will_paginate'
Any idea why I'm getting an error?
<%= will_paginate #root_comments,:param_name => 'discussions_page', renderer: BootstrapPagination::Rails %>
I am using Rails 3.2.14, thinking-sphinx 3.1.1 and Sphinx 2.2.4-id64-release (r4806)
I have two modals below:-
Album.rb
has_and_belongs_to_many :genres
Genre.rb
has_and_belongs_to_many :albums
So the associated table is albums_genres
I want to search albums using the genre_id
Example: When I pass a genre_id then it should return all the albums associated to that genre_id on the basis of third table albums_genres
I have tried like that
ThinkingSphinx::Index.define :album, :with => :active_record do
#other indexes
has genres.id, :as => :genre_ids, :source => :query
end
But when i run the rake task as rake ts:index i am getting the error as:-
rake aborted!
undefined method `join_table' for #<ActiveRecord::Reflection::AssociationReflection:0x000000079224d8>
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:15:in `quoted_foreign_key'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:28:in `to_sql'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:76:in `queries'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:5:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:15:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:45:in `query'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:38:in `multi_declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:23:in `declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:96:in `block in append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `each'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:132:in `prepare_for_render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:65:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/index.rb:53:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:90:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `block in render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:13:in `configure'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:24:in `index'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `eval'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `<main>'
It looks like this is a bug with Rails 3.2, :source => :query and HABTM joins (which is sadly not surprising - getting the code to work even with Rails 4.0 was painful and fragile).
I'm afraid your options are either upgrade to Rails 4 or not use the :source => :query option (and I know that means you lose some nice speed gains for indexing).
Odds of me getting a patch sorted in the near future is small (but others are welcome to give it a shot, if they wish).
I keep getting the error ArgumentError: wrong number of arguments (0 for 1) for my default_scope which is default_scope { where("#{table_name}.tenant_id IS NULL") }
It keeps giving me this error and I don't understand why. I have the default scope in my users model.
Update:
Error output if using rails console:
ArgumentError: wrong number of arguments (0 for 1)
from /home/evan/Apps/demo-application/app/models/user.rb:18:in `hash'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping.rb:64:in `value_for'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-4.0.4/lib/active_support/per_thread_registry.rb:40:in `public_send'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-4.0.4/lib/active_support/per_thread_registry.rb:40:in `block in method_missing'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:123:in `ignore_default_scope?'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:134:in `evaluate_default_scope'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/scoping/default.rb:110:in `build_default_scope'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:554:in `with_default_scope'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:582:in `exec_queries'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:471:in `load'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:220:in `to_a'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.4/lib/active_record/relation.rb:573:in `inspect'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /home/evan/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
The hash is in my user model as below.
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.hash(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.hash(User.new_remember_token)
end
Error output if using rails server:
ArgumentError - wrong number of arguments (0 for 1):
activerecord (4.0.4) lib/active_record/scoping.rb:70:in `set_value_for'
activesupport (4.0.4) lib/active_support/per_thread_registry.rb:40:in `block in method_missing'
activerecord (4.0.4) lib/active_record/scoping/default.rb:127:in `ignore_default_scope='
activerecord (4.0.4) lib/active_record/scoping/default.rb:140:in `ensure in evaluate_default_scope'
activerecord (4.0.4) lib/active_record/scoping/default.rb:140:in `evaluate_default_scope'
activerecord (4.0.4) lib/active_record/scoping/default.rb:110:in `build_default_scope'
activerecord (4.0.4) lib/active_record/relation.rb:554:in `with_default_scope'
activerecord (4.0.4) lib/active_record/relation.rb:582:in `exec_queries'
activerecord (4.0.4) lib/active_record/relation.rb:471:in `load'
activerecord (4.0.4) lib/active_record/relation.rb:220:in `to_a'
activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:316:in `find_take'
activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:66:in `take'
activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:49:in `find_by'
activerecord (4.0.4) lib/active_record/querying.rb:6:in `find_by'
app/helpers/sessions_helper.rb:16:in `current_user'
app/helpers/sessions_helper.rb:19:in `signed_in?'
And the relevant rails methods:
def ignore_default_scope? # :nodoc:
ScopeRegistry.value_for(:ignore_default_scope, self)
end
def ignore_default_scope=(ignore) # :nodoc:
ScopeRegistry.set_value_for(:ignore_default_scope, self, ignore)
end
You should avoid overwriting Ruby core methods like Object#hash, also considering that Object#hash is an essential method in Ruby. From the docs:
Generates a Fixnum hash value for this object. This function must have
the property that a.eql?(b) implies a.hash == b.hash.
The hash value is used along with eql? by the Hash class to determine
if two objects reference the same hash key. Any hash value that
exceeds the capacity of a Fixnum will be truncated before being used.
The hash value for an object may not be identical across invocations
or implementations of ruby. If you need a stable identifier across
ruby invocations and implementations you will need to generate one
with a custom method.
If you really have to overwrite core methods you should guarantee their functionality and do not change their arguments: you will get unexpected behaviours all over your app otherwise, like the error you're referring.
I think that you have not such a variable or method as table_name
I think this will work:
table name = "users"
default_scope { where("#{table_name}.tenant_id IS NULL") }
Also it is cleaner to use callable objects for scopes (they are waiting for it), so it is better to use lambda here:
table name = "users"
default_scope ->{ where("#{table_name}.tenant_id IS NULL") }
# or old syntax:
default_scope lambda { where("#{table_name}.tenant_id IS NULL") }
I am using friendly_id 4.0.0 rails 3.0.1 and tiny_tds 0.2.3 (because I have a sql server database as a backend for the rails application)
In my model I have
extend FriendlyId
friendly_id :friendly_name, :use => [:slugged]
When I try to create a new record for the class from rails console, I get the following error:
ActiveRecord::StatementInvalid: TinyTds::Error: 'LENGTH' is not a recognized built-in function name.: SELECT TOP (1) [j_service_provider].* FROM [j_service_provider] WHERE ([slug] = N'' OR [slug] LIKE N'--%') ORDER BY LENGTH([slug]) DESC, [slug] DESC
This is because SQL Server does not have the LENGTH function, but is has a LEN function.
So if I change the query on line 48 in slug_generator.rb in conflicts method to:
scope = scope.order("LEN(#{column}) DESC, #{column} DESC")
the create works.
However to generate slugs for existing records in database if I run the find_each(&:save) it still does not work.
Is there some other configuration change needed to make friendly_id work with sql server using tiny_tds?
This is the stack trace:
ActiveRecord::StatementInvalid: TinyTds::Error: 'LENGTH' is not a recognized built-in function name.: SELECT TOP (1) [company].* FROM [company] WHERE ([slug] = N'' OR [slug] LIKE N'--%') ORDER BY LENGTH([slug]) DESC, [slug] DESC
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `log'
from /usr/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-3.0.7/lib/active_record/connection_adapters/sqlserver/database_statements.rb:249:in `raw_select'
from /usr/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-3.0.7/lib/active_record/connection_adapters/sqlserver/database_statements.rb:193:in `select'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/base.rb:467:in `find_by_sql'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation.rb:64:in `to_a'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/finder_methods.rb:333:in `find_first'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/relation/finder_methods.rb:122:in `first'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slug_generator.rb:38:in `conflict'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slug_generator.rb:33:in `conflict?'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slug_generator.rb:23:in `generate'
from /usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.0/lib/friendly_id/slugged.rb:257:in `set_slug'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:424:in `_run_validation_callbacks'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.1/lib/active_model/validations/callbacks.rb:67:in `run_validations!'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.1/lib/active_model/validations.rb:179:in `valid?'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/validations.rb:55:in `valid?'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/validations.rb:75:in `perform_validations'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/validations.rb:43:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/attribute_methods/dirty.rb:21:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:237:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:289:in `with_transaction_returning_status'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:204:in `transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:237:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:248:in `rollback_active_record_state!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/transactions.rb:236:in `save'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.1/lib/active_record/base.rb:498:in `create'
Sql server is not currently supported for friendly id as per this issue.
https://github.com/norman/friendly_id/issues/214#issuecomment-3528842
So it appears they've made a change. So you need to specify the beta version in your gemfile.
Like so...
gem 'friendly_id', '4.1.0.beta.1'
Then build install, restart your web server and you're good to go!