Active Model Serializer issue for Nil object - ruby-on-rails

ActiveModelSerializers::SerializableResource.new(object.project, include: [collaborator: :pictures]).as_json[:project]
When object.project is nil then it occurs an issue.
NoMethodError: undefined method `[]' for nil:NilClass

It's not related to ActiveModelSerializers (AMS) after all. In Ruby, nil causes a lot of NoMethodError, so we often avoid nils with if or unless modifiers.
For example, in your case, we often do
ActiveModelSerializers::SerializableResource.new(object.project, include: [collaborator: :pictures]).as_json[:project] if object.project

Related

STI wth Strong params undefined local variable or method `attributes'

undefined local variable or method `attributes' for #CarsController:0x00007fa686991b30
def cars_params(type)
params.require(type.to_sym).permit(attributes)
end
My model is CommonCar::RedTrunk, so type has become "common_car_red_trunk"
I am using STI and following this.. https://gist.github.com/danielpuglisi/3c679531672a76cb9a91
I don't really understand why attributes was used and why its now failing. I assumed it maybe took the attributes from the required model, but not sure. Any insights would be helpful to get this working.

effectively applying nil check in ruby

I have this code
args = x.lead_details.last.leads.last.country_id rescue nil
Function(args)
I have used rescue keyword to make sure I don't get errors like
undefined method `country_id' for nil:NilClass,
undefined method `leads' for nil:NilClass
Is there any better way I can achieve this?
Other approach is
Function(x.lead_details.last.leads.last.country_id if x.present? && x.lead_details.present? && x.lead_details.last? && so on)
rescue nil is evil. It does not just rescue nil errors. It eats everything you throw at it. For example:
class Foo
def bar
end
def woo
raise ArgumentError
end
end
Foo.new.baz rescue nil # nil!
Foo.new.woo rescue nil # nil!
This applies to any inline rescue.
Ruby 2.3.0 introduced the safe navigation operator that lets you do:
x&.lead_details&.last&.leads&.last&.country_id
ActiveSupport also has a Object#try! method that can be used in older versions of Ruby:
x.try!(:lead_details).try!(:last).try!(:leads).try!(:last).try!(:country_id)
But you're really just pissing on the Law of Demeter here and you should refactor the code so you don't have to traverse down that insane method chain as you're really just burying tons of potential bugs and making debugging impossible.
See:
5 reasons not to use safe navigation operators
You can use the safe navigation operator (&.) that was introduced in Ruby 2.3:
Function(x&.lead_details&.last&.leads&.last&.country_id)
It returns nil when you try to call a method on nil.

Upgraded to ActiveAdmin 1.0. Getting error undefined method `exclude_contest_eq' for Ransack::Search

I just upgraded to rails 4 and ActiveAdmin 1.0. I'm having problem with page that won't work anymore. Here is the error:
undefined method `exclude_contest_eq' for Ransack::Search<class: Project, base: Grouping <combinator: and>>:Ransack::Search
Here is where the code is failing
controller do
def render(*args)
#projects.uniq! if #projects and action_name == 'index'
super(*args)
end
end
What has changed that is throwing this error?
It has something to do with filtering.
Here is the thread with long discussion of the issue.
The essence of it, that quickest fix would be, probably, to add remove_filter :exclude_contest to the model definition.
May this will solve your problem as well.

Rails 3 undefined method `create' for nil:NilClass error while trying create a related object

I have two models, User and Profile, in one-to-one relation and I am trying to create a new profile for a user if it does not exist yet:
user = User.includes(:profile).find( params[:user_id] )
unless user.profile.present?
user.profile.create
end
But I am getting an error: undefined method `create' for nil:NilClass
Well, two things. Firstly, I assume the code is wrong, as that only enters the block if the profile is there (and hence can't create it).
if user.profile.blank?
user.profile.create
end
looks like more correct code.
Secondly, when you use a has_one, you don't use .create like you do with has_many. This is because the relation object is directly returned, and not a "proxy" method like a has_many. The equivalent method is create_profile (or create_x where x is the object)
Hence, try the following code:
if user.profile.blank?
user.create_profile
end

NoMethodError User Authentication

I'm trying out user authentication for the first time and am running to a bit of an issue.
I've defined my make_salt method in my AdminUser model
def self.make_salt(username="")
Digest::SHA1.hexdigest("Use #{username} and other stuff")
end
Then in my console when I run AdminUser.make_salt, I get:
>> AdminUser.make_salt
NoMethodError: undefined method `make_salt' for #<Class:0x1063ddb58>
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/base.rb:1009:in `method_missing'
from (irb):30
Also, I had a hash method defined, which worked fine, but when I changed it to hash_with_salt I also got a NoMethodError.
Am I just missing something obvious here?
Ugh. Finally figured out this one. I simply needed to restart my console after saving the changes to my code.

Resources