Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Rails.env in a model results in an error #16

Closed
thomaswitt opened this issue Nov 27, 2024 · 7 comments · Fixed by #17
Closed

Accessing Rails.env in a model results in an error #16

thomaswitt opened this issue Nov 27, 2024 · 7 comments · Fixed by #17
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@thomaswitt
Copy link

Describe the bug

I can't use Rails.env (e.g. Rails.env.test?) anymore in Models, resulting in:

     Failure/Error: return if Rails.env.test?

     NoMethodError:
       undefined method `env' for module Aws::Record::Rails
     # ./app/models/user.rb:121:in `email_is_unique'
     # ./spec/models/user_spec.rb:30:in `block (3 levels) in <top (required)>'

Only when I use ::Rails.env.test? it works.

Expected Behavior

Rails.env should have the expected behavior

Current Behavior

Rails.env is not available, only ::Rails.env

Reproduction Steps

Access Rails.env in any model.

Possible Solution

No response

Additional Information/Context

No response

Gem name ('aws-sdk', 'aws-sdk-resources' or service gems like 'aws-sdk-s3') and its version

aws-record-rails latest

Environment details (Version of Ruby, OS environment)

Ruby 3.3.6 macos

@thomaswitt thomaswitt added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 27, 2024
@mullermp
Copy link
Contributor

Could you please share your model and example code causing this?

@thomaswitt
Copy link
Author

class User < BaseModel
  include Aws::Record
  include ActiveModel::Validations

  string_attr :user_email
  string_attr :name
  string_attr :first_name
  string_attr :last_name
  string_attr :language
  string_attr :telegram_username

  validates_presence_of :user_email
  validates_presence_of :name
  validate :test_me

  global_secondary_index(:user_email_gsi,
                         hash_key: :user_email,
                         projection: { projection_type: 'ALL' })

  # FIXME https://github.com/aws/aws-sdk-ruby-record/issues/143
  # global_secondary_index(:telegram_username_gsi,
  #                        hash_key: :telegram_username,
  #                        projection: { projection_type: 'ALL' })

  def test_me
    return if Rails.env.test?
    errors.add(:user_email, "can't be blank") if user_email.blank?
  end
end

@mullermp
Copy link
Contributor

Ok. So the issue is definitely with my refactoring: https://github.com/aws/aws-record-rails/blob/main/lib%2Faws-record-rails.rb#L17

Option 1 - Remove the rails module, just add a railtie, and put the version under that. This may be confusing and conflict with the aws-record gem version.

Option 2 - Acknowledge this as breakage and customers roll forward small fixes. Using a root namespace is often necessary and preferential.

Option 3 - Similar to 2, drop this gem if you don't use it. Requires upgrading to aws-sdk-rails 5. It is included by default in 4.2 to avoid breaking change in a minor version.

@thomaswitt
Copy link
Author

@mullermp I am actually using aws-sdk-rails. Do I need to remove gem 'aws-record-rails' from my gemfile? I thought with v5 I HAVE to actively add it, like i also do with aws-activejob-sqs.

@mullermp
Copy link
Contributor

No, with v5 all of the gems are modular. If you are not using sqs active job or the aws-record model/scaffold generators, you can remove them from your Gemfile.

I think for your cases, you are using only aws-record and aws-actiondispatch-dynamodb.

@mullermp
Copy link
Contributor

Please see aws/aws-sdk-rails#165

If there are any confusing bits I can clarify them in the post.

As far as a fix for this, let me think about whether to remove the rails namespace or not..

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants