Questions tagged [activemodel]

A toolkit for building modeling frameworks like Active Record. Rich support for attributes, callbacks, validations, serialization, internationalization, and testing.

ActiveModel brings many of ActiveRecord's features (such as validations and callbacks) to non-ActiveRecord classes.

707 questions
1
vote
1 answer

Active Record: NameError (undefined local variable or method `attributes' for nil:NilClass Did you mean? attribute_names)

I recently migrated from Rails 6.1 to 7.0 and I keep getting the error when I try to sign up a user. NameError (undefined local variable or method `attributes' for nil:NilClass Did you mean? attribute_names): activemodel (7.0.2.2)…
1
vote
1 answer

NameError undefined local variable or method `primary_abstract_class' for #

I'm trying to run rails with jruby rails : 6.1.4.4 | jruby : 9.3.3.0 | ruby : 2.6.8 I get this error for when i try to use any Model(s) In rails console User.all.limit(1) Traceback (most recent call last): 4: from (irb):2:in `evaluate' …
Suhas C V
  • 104
  • 1
  • 5
1
vote
1 answer

Rails Model.update deletes nested attachment

I have a Rails 6 app. User has_one Shop Shop has_one_attached Logo User.update with nested_attributes purges the User.shop.company_logo. class User has_one :shop, dependent: :destroy # delete Shop if user gets deleted …
Jan
  • 12,992
  • 9
  • 53
  • 89
1
vote
1 answer

Default ActiveRecord/ActiveModel ::Errors are anonymous

Default ActiveModel::Errors are great, but i am solving problem, that the messages are anonymous. For example there is message should look like an email address. that belongs to email field, but what i want is to know that this error message is…
Schovi
  • 1,960
  • 5
  • 19
  • 33
1
vote
2 answers

ActiveModel: Disable failing validation when associated models fail

Does Rails3 always run validates_associated against all models by default? In a simple setup like this class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post def validate …
1
vote
2 answers

How to override ActiveRecord CRUD operations?

Is it possible that I create my model using "rails generate model..." which creates all them CRUD stuff including views for create, update and delete, AND then I override rails CRUD methods to do something else instead for example printing them all…
Bilal Wahla
  • 665
  • 1
  • 12
  • 28
1
vote
2 answers

How can I use Rails Validations to allow and attribute variable to be one of two values?

For a project, I need the categories attribute of the Post class to be either "Fiction" or "Non-Fiction." I attempted the following syntax... class CategoryValidator < ActiveModel::Validator def validate(record) unless (record.category…
user15217186
1
vote
1 answer

Modifying an ActiveRecord module to work with any model

So a while ago I created a small module to serve as the methods I need for a votable polymorphic association, and while it was originally meant to be used only for ActiveRecord I now want to use it with mongo, and since I'm using mongoid all of the…
JP Silvashy
  • 46,977
  • 48
  • 149
  • 227
1
vote
1 answer

Check Attachment is updated or not in parent model for Active storage attachments

I am migrating the attachments from paperclip to Active storage. In paperclip, we can check whether the file is updated by using the active model dirty as below, after_update_commit :notify_users, if: :saved_change_to_file_updated_at? But in…
Saranya
  • 41
  • 4
1
vote
1 answer

Rails way of hiding things that are unpublished

I'm a bit stuck with something that I've previously done countless times in Node or Django. I'm building an API-only Rails app. Say I have a Post model containing blog posts. The model has a published attribute, which determines if a certain post is…
zcserei
  • 577
  • 7
  • 30
1
vote
1 answer

Is there a simple way to convert an ActiveRecord object into an ActiveModel object?

I've been reading about the repository pattern: https://blog.spacesnottabs.io/2019/01/30/the-repository-pattern-for-ruby/ and it would be great to have the ability to convert an ActiveRecord object and convert it to an ActiveModel object so that it…
stwr667
  • 1,566
  • 1
  • 16
  • 31
1
vote
1 answer

How to forward model validation errors to a view based on another model in Rails?

I created a data import page to centralize the feature, and remove numerous import controllers. I have a DataImport object to grab parameters for the DataImports controller. The controller identifies the required importation model, and kicks the…
user1185081
  • 1,898
  • 2
  • 21
  • 46
1
vote
1 answer

Getting an error with custom validation in a Rails 3 ActiveModel

I'm trying to use the Date Validator Gem but I am running into an error and I'm not sure if it's because the model isn't Active Record or not (I've seen people suggest that validation is a bit funky in ActiveModel when not in ActiveRecord). I am…
Chance
  • 11,043
  • 8
  • 61
  • 84
1
vote
2 answers

Should I validate the type column? (Rails, STI)

I have a model and I am using Single Table Inheritance. This has a type column in my database which is populated with the string of the class name. Should I validate this column? Options: required column in the db validates :type, presence:…
1
vote
1 answer

Clean way to return ActiveRecord object with associations

I want to return all the Thing model objects with just the associations without the asscoiation_id, is there a better way to do this without include and except? # Thing.rb belongs_to :object_a belongs_to :object_b # create_thing.rb def change …
AirWick219
  • 894
  • 8
  • 27