Skip to content

Fix ArgumentError in Rails 3.2 - #116

Open
ryu39 wants to merge 1 commit into
mbleigh:masterfrom
ryu39:feature/fix_argment_error_in_rails_3
Open

Fix ArgumentError in Rails 3.2#116
ryu39 wants to merge 1 commit into
mbleigh:masterfrom
ryu39:feature/fix_argment_error_in_rails_3

Conversation

@ryu39

@ryu39 ryu39 commented Oct 16, 2016

Copy link
Copy Markdown

This PR fixes #115.

In Rails 3.2, ActiveRecord::FinderMethods#take does not exist ( added since Rails 4.0 http://apidock.com/rails/ActiveRecord/FinderMethods/take ), and ActiveRecord::Relation#take is processed as 'to_a.take' ( https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/relation/delegation.rb#L40 ).
Array#take requires 1 argument ( https://ruby-doc.org/core-2.2.0/Array.html#method-i-take ), but no arguments are passed in SeedFu::Seeder#find_or_initialize_record, so ArgumentError is raised.

I have 3 ideas to fix this issue.

  1. Revert take to first.
  2. Use take(1).first instead of take.
  3. Use limit(1).to_a.first instead of take.

I choose No. 3 because the SQL is same in Rails 3.2 and 4.0. And I also consider Pull #89.

User.where(name: 'Name').first
# Rails 3.2.22.5
#   => SELECT "users".* FROM "users" WHERE "users"."name" = 'Name' LIMIT 1
# Rails 4.0.13
#   => SELECT "users".* FROM "users" WHERE "users"."name" = 'Name' ORDER BY "users"."id" ASC LIMIT 1

User.where(name: 'Name').take(1).first
# Rails 3.2.22.5
#    => SELECT "users".* FROM "users" WHERE "users"."name" = 'Name'
# Rails 4.0.13
#    => SELECT "users".* FROM "users" WHERE "users"."name" = 'Name' LIMIT 1

User.where(name: 'Name').limit(1).to_a.first
# Rails 3.2.22.5
#    => SELECT "users".* FROM "users" WHERE "users"."name" = 'Name' LIMIT 1
# Rails 4.0.13
#    => SELECT "users".* FROM "users" WHERE "users"."name" = 'Name' LIMIT 1

If implicit order of ActiveRecord::FinderMethods#first (>= Rails 4.0) can be ignored, I think No. 1 may be better because it is easy to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Version 2.3.6 does not work in Rails 3.2

1 participant