fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

has_one and has_many association throw[ wrong number of arguments (given 1, expected 0)] when passed with proc shortcut

Open mayankkumarji opened this issue 6 years ago • 6 comments

I had rails 4.2 and ruby 2.3.5 and I'm upgrading it to rails 5.2 with ruby 2.6.1. With older rails version(4.2) my UserAuthenticatedSerializer is working fine and was giving proper response. After upgrading to rails 5.2 my Serializer throwing argument error. I debugged it and it's throwing from relationship.rb file, method name fetch_id and fetch_associated_object Here in both methods, object_block.call(record, params) throwing me argument error. If I remove second parameter params it works fine but passing this two arguments causing me error. This same association work with rails 4.2 but it's not working in rails 5.2. Here is my code snap : response = UserAuthenticatedSerializer.new(@user, { params: { domain: current_domain } }).to_json error = ArgumentError (wrong number of arguments (given 1, expected 0)) #user_authenticated_serializer.rb ===>

class UserAuthenticatedSerializer
  include FastJsonapi::ObjectSerializer
  has_one :user_profile, serializer: UserProfileSerializer, &:user_profile
  has_many :user_topic_label_order, &:user_topic_label_order
end

#user.rb ====> Relationship in user model:

has_one  :user_profile, dependent: :destroy
has_many :user_topic_label_order, dependent: :destroy

Rails version : 5.2.2 Ruby version : ruby 2.6.1 fast_jsonapi gem version : fast_jsonapi (1.5)

mayankkumarji avatar Mar 08 '19 08:03 mayankkumarji

Hi @mayankkumarji,

Could you explain what are &:user_topic_label_order and &:user_profile params being passed for? I have a feeling those are causing issues.

shishirmk avatar Mar 18 '19 15:03 shishirmk

Hi @shishirmk : Yeah, I have explained in title that this proc shortcuts(&:user_profile, &:user_topic_label_order) is causing me error, you can see both are the associate model with User model and relation is like has_one :user_profile, dependent: :destroy has_many :user_topic_label_order, dependent: :destroy

mayankkumarji avatar Mar 19 '19 06:03 mayankkumarji

@mayankkumarji this is going to be difficult to debug without looking at the proc definitions and more details. Also you never changed the version of fast_jsonapi? Was it always 1.5?

shishirmk avatar Mar 29 '19 04:03 shishirmk

@shishirmk : Okay, I need to provide you a working demo. Yes version of fast_jsonapi was always 1.5.

mayankkumarji avatar Apr 01 '19 15:04 mayankkumarji

@shishirmk : Please find working demo here fast-json I have created.

Setup Instruction link and issue reproduce https://github.com/mayankkumarji/fast-json-demo/wiki/Setup-Instruction

mayankkumarji avatar Apr 02 '19 07:04 mayankkumarji

I was able to reproduce the issue. I am looking into it. It might have been a change in behavior from active record. Why are you passing the proc? It looks like it works if you just don't pass the proc at all. If you dont have custom logic in this proc i don't understand the need to even pass it.

class UserAuthenticatedSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :address

  has_one :user_profile
end

shishirmk avatar Apr 07 '19 19:04 shishirmk