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!
Related
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") }
User has_many comments. Comment are in the comments table (polymorphic).
User.where("comments.commentable_type = ? AND comments.commentable_id = ?", "Post", post.id).uniq
I get:
Post Load (0.4ms) SELECT `posts`.* FROM `posts` LIMIT 1
TypeError: Cannot visit Arel::Nodes::Distinct
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:25:in `rescue in visit'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:133:in `visit_Arel_Nodes_SelectCore'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:41:in `visit_Arel_Nodes_SelectCore'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `block in visit_Arel_Nodes_SelectStatement'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `map'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `visit_Arel_Nodes_SelectStatement'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:36:in `visit_Arel_Nodes_SelectStatement'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:5:in `accept'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:19:in `accept'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/bind_visitor.rb:11:in `accept'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `to_sql'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/explain.rb:40:in `logging_query_plan'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/querying.rb:37:in `find_by_sql'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:171:in `exec_queries'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:160:in `block in to_a'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/explain.rb:33:in `logging_query_plan'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:159:in `to_a'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:496:in `inspect'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
Without .uniq, it works, except I get duplicates, therefore, I can try:
User.where("comments.commentable_type = ? AND comments.commentable_id = ?", "Post", post.id).to_a.uniq
And it will work...but I don't like that method! Is there any other way?
Restart the rails server session.
This is a link to the github issue related to the same problem that you face.
If you are using rails 4 try using .distinct instead of .uniq .
I remember having issues similar, couple things you can do. First use the SQL distinct (I know it is suppose to be similar to the uniq). Second (I know this is not the best way), you can just select it all then just use regular array uniq functions to filter it just like how you would filter a normal array (this is to just get things going).
I have a Purchase model which has_one Coupon.
The Purchase model contains this code:
def decrement_coupon
coupon = Coupon.find_by_code(coupon_code, :readonly => false)
return unless coupon.respond_to?(:uses)
coupon.uses = coupon.uses - 1
coupon.save
end
But I got this error the other day:
ActiveRecord::ReadOnlyRecord - ActiveRecord::ReadOnlyRecord:
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/persistence.rb:245:in `create_or_update'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/callbacks.rb:273:in `create_or_update'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activesupport-3.0.6/lib/active_support/callbacks.rb:419:in `_run_save_callbacks'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/callbacks.rb:273:in `create_or_update'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/persistence.rb:39:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/validations.rb:43:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:240:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:207:in `transaction'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:240:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:239:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/associations/association_proxy.rb:222:in `send'
/usr/local/rvm/gems/ree-1.8.7-2010.02#lyconic/gems/activerecord-3.0.6/lib/active_record/associations/association_proxy.rb:222:in `method_missing'
/var/www/88tactical_production/releases/20110823232510/app/models/purchase.rb:69:in `decrement_coupon'
Does :readonly => false not guarantee a non-readonly record? Would moving this function to the Coupon model make a difference? (I'll probably do that regardless)
This is probably due to joining in other child values through your find_by_code() helper.
When ActiveRecord uses joins to lookup an object returning additional values, it marks the record read-only.
There's a thorough description of this on this question here:
What is causing this ActiveRecord::ReadOnlyRecord error?
If this is the case, you can probably fix the issue by turning a your :joins into a proper :include that populates the ActiveRecord instances without marking them read-only.
I'm trying to upgrade from 2.3.8 to 3.0.1, but this problem has been plaguing me and preventing further progress for quite some time now. Any attempt to create something from my models (calling Subject.create! from the console, for example) always results in this error:
NoMethodError: undefined method `name' for 0:Fixnum
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `block in create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `each'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `detect'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/algebra/relations/writes.rb:24:in `call'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/session.rb:17:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/algebra/relations/relation.rb:159:in `insert'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/relation.rb:14:in `insert'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:271:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/timestamp.rb:47:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:281:in `block in create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:413:in `_run_create_callbacks'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:281:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:247:in `create_or_update'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:277:in `block in create_or_update'
from /.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:413:in `_run_save_callbacks'
... 1 levels...
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:56:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:49:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:242:in `block in save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:204:in `transaction'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:242:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:34:in `create!'
from (irb):1
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:44:in `start'
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:8:in `start'
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.2-p0
It would appear Arel is not getting a proper relation to work with, but I am lost as to what is causing this. Any ideas?
Thanks!
Ryan
What version of Ruby are you using? It should be either 1.8.7 or 1.9.2. I've had similar inexplicable issues with 1.9.1.
I would recommend putting some debug statements inside arel. Edit the engine.rb in the last line of the stack trace. I think you will have to restart the console (not just reload!) to see the changes in the arel file.
Here is the offending line in engine.rb:
attribute = relation.record.detect { |attr, _| attr.name.to_s == relation.primary_key.to_s }
Maybe try printing out the attr names and see if anything is passing thru the array detect method. Or print out the class of the "attr". The class should be "Arel::Attribute", but obviously a Fixnum is passing in there somehow.
attribute = relation.record.detect do |attr, _|
p attr.class
p attr.name
attr.name.to_s == relation.primary_key.to_s
end
Just a guess but I am thinking maybe a plugin/gem is interfering with something in your models. Are you using any dependencies that tie directly into ActiveRecord?
Another good exercise would be to create a blank Rails 3 app (with no gems or plugin dependencies) with one model and see if you get the same errors. That would rule out something in your development environment. Then if that works, then maybe you can start removing some of the dependencies one by one from the other app and see if one of them is causing the error.