Am using the save option on mongoid on ruby on rails, its throwing me an error
can't convert Fixnum into String
my model file are
product.rb
class Product
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Versioning
field :product_role, :type => String
field :status_id, type: Moped::BSON::ObjectId
field :status_name,type: String
end
status.rb
class Status
include Mongoid::Document
field :name, type: String
index :name => 1
end
controller code is
product_id = "523741002a6c6e8d1a000002",
status_name = "Pending Review"
product_detail_status = Product.where(id:product_id).first
status = Status.where(name:status_name).first
puts "##############"
puts product_detail_status.id.to_s
puts status.name.to_s
puts status.id.to_s
puts "##############"
product_detail_status.status_name = status.name.to_s
product_detail_status.status_id = status.id
product_detail_status.save
i can able to view the values in puts, but its not saving by the way its throwing error on
product_detail_status.save as "can't convert Fixnum into String"
Completed 500 Internal Server Error in 657ms
TypeError - can't convert Fixnum into String:
mongoid (3.1.4) lib/mongoid/versioning.rb:53:in `revise'
activesupport (3.2.13) lib/active_support/callbacks.rb:407:in `
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (3.1.4) lib/mongoid/callbacks.rb:130:in `run_callbacks'
mongoid (3.1.4) lib/mongoid/persistence/modification.rb:24:in `prepare'
mongoid (3.1.4) lib/mongoid/persistence/operations/update.rb:45:in `persist'
mongoid (3.1.4) lib/mongoid/persistence.rb:145:in `update'
mongoid (3.1.4) lib/mongoid/persistence.rb:82:in `save'
thanks in advance
I'm not sure why you are getting that error, but a better approach may be to use relations instead of manually adding a status_id and status_name to the product model
class Product
include Mongoid::Document
field :product_role, :type => String
has_one :status
end
Then in your controller, assign the Product's status as such
product_detail_status = Product.where(id:product_id).first
status = Status.where(name:status_name).first
product_detail_status.status = status
i found the solution, but am not sure its right way, on product model i remove
include Mongoid::Versioning
by that i can save it on product model by command save.
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 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 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
I am new to rails and am following the Depot application in the Pragmatic Agile Web development with rails and I am having an odd problem.
In my product model I created a validator for confirming that the image asked for in the image url field actually exist as an asset. Here is my product model code.
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
validates :description, :price, :title, :presence => true
validate :image_url_exists, on: :create
def image_url_exists
if Rails.application.assets.find_asset(image_url) == nil
errors.add(:image_url, 'is not valid. The image does not exist.')
end
end
end
Now the problem is when I run my unit test. Here is what it is:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "the products attributes should not be empty" do
p = Product.new
assert p.invalid?
end
end
But doing so triggers a bunch of errors in my code. Without the custom validtor, everything seems to work just fine. Here are the errors I am currently getting.
test_the_products_attributes_should_not_be_empty(ProductTest):
TypeError: can't convert nil into String
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `initialize'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `new'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `find_asset'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in `find_asset'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/environment.rb:78:in `find_asset'
/home/saurabh/Desktop/SCRIPTS/Rails/depot/app/models/product.rb:6:in `image_url_exists'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:418:in `_run__4343689776242734370__validate__107120755283260520__callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:228:in `run_validations!'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:403:in `_run__4343689776242734370__validation__107120755283260520__callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:195:in `valid?'
/var/lib/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/validations.rb:69:in `valid?'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:203:in `invalid?'
The problem is Rails.application.assets.find_asset(image_url), image_url is nil when you create a refresh object at test:
p = Product.new # Here p.image_url => nil
You can find the reason of the error looking at log
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `initialize'
Here (link) the line 156 where error happens, path was nil here.
So to solve this problem you need to add a condition at your if to check if image_url isn't nil, here is my sugestion
if self.image_url.nil? || Rails.application.assets.find_asset(self.image_url).nil?
errors.add(:image_url, 'is not valid. The image does not exist.')
end
Rails 3.1.2 with mail gem 2.3.0
I'm trying to use ActionMailer with an ActiveModel based form. I've defined my table less model like so:
class StprodApp
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :email,:name
attr_reader :errors
validates_presence_of :email, :message => "Please enter your email address"
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
#errors = ActiveModel::Errors.new(self)
end
def persisted?
false
end
end
So far so good, I can post a form based on stprod_app, and validations work
So now I want to send an email based off this model, so I do this in my create action:
#stprod_app = StprodApp.new(params[:stprod_app])
if #stprod_app.valid?
EmployeeMailer.stprod_app(#stprod_app).deliver
end
I get this error
undefined method `index' for #<StprodApp:0x007f95325a1ad0>
and this stack trace
mail (2.3.0) lib/mail/encodings.rb:117:in `value_decode'
mail (2.3.0) lib/mail/encodings.rb:101:in `decode_encode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:74:in `do_decode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:56:in `decoded'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:107:in `wrapped_value'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:70:in `do_encode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:52:in `encoded'
mail (2.3.0) lib/mail/field.rb:123:in `method_missing'
mail (2.3.0) lib/mail/header.rb:190:in `block in encoded'
mail (2.3.0) lib/mail/header.rb:189:in `each'
mail (2.3.0) lib/mail/header.rb:189:in `encoded'
mail (2.3.0) lib/mail/message.rb:1708:in `encoded'
actionmailer (3.1.2) lib/action_mailer/base.rb:451:in `set_payload_for_mail'
actionmailer (3.1.2) lib/action_mailer/base.rb:431:in `block in deliver_mail'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `block in
instrument'
activesupport (3.1.2) lib/active_support/notifications/instrumenter.rb:21:in
`instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `instrument'
actionmailer (3.1.2) lib/action_mailer/base.rb:430:in `deliver_mail'
mail (2.3.0) lib/mail/message.rb:230:in `deliver'
app/controllers/stprod_app_controller.rb:20:in `create'
So I read the docs for the mail gem, and it says this:
---snip---
All objects that can render into an email, have an #encoded method. Encoded will return the object as a complete string ready to send in the mail system, that is, it will include the header field and value and CRLF at the end and wrapped as needed.
All objects that can render into an email, have a :decoded method. Decoded will return the object's "value" only as a string. This means it will not include the header fields (like 'To:' or 'Subject:').
By default, calling #to_s on a container object will call its encoded method, while #to_s on a field object will call it's decoded method. So calling #to_s on a Mail object will return the mail, all encoded ready to send, while calling #to_s on the From field or the body will return the decoded value of the object. The header object of Mail is considered a container. If you are in doubt, call #encoded, or #decoded explicitly, this is safer if you are not sure.
Structured fields that have parameter values that can be encoded (e.g. Content-Type) will provide decoded parameter values when you call the parameter names as methods against the object.
Structured fields that have parameter values that can be encoded (e.g. Content-Type) will provide encoded parameter values when you call the parameter names through the object.parameters[''] method call.
---snip---
It looks like the index error is Rail's last desperate attempt to decode/encode? the email. The only 'index' I found in rails that makes sense is in ActiveSupport::Multibyte::Chars.
It sounds like I have to implement an encode/decode method in my ActiveModel, but I can't figure out how!
Anybody know how to define an ActiveModel that works with ActionMailer?
Solved, left off a crucial detail, and therin lay the problem
I was passing the Stprod_app instance variable to the mail call:
def stprod_app(emp,app)
#app = app
#emp = emp
mail(:to=> #app.antibot,
:from=>#emp.email,
:app=>#app) # <<< DOH!!!!
Sorry!
I had a similar error from this code:
def report( request )
headers['environment'] = RAILS_ENV
headers['identifier'] = request.id
...
end
In this case, it could not encode request.id because it was numeric. Changing it to request.id.to_s solved the problem.