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
3
votes
1 answer

Rails 2.3.11 Create Model for Form And Use ActiveRecord Validation

In Rails 3 you simply include ActiveRecord modules in order to add validations to any non-database backed model. I want to create a model for a form (e.g. ContactForm model) and include ActiveRecord valiations. But you cannot simply include the…
user791715
  • 123
  • 1
  • 5
3
votes
1 answer

Copying / Cloning an ActiveStorage attribute

I want to clone an attached image to another model. Either as: a reference, such that the attachment won't be deleted until all the referring objects are deleted by cloning the attachment and having it as a distinct replica... How does rails…
Hari Honor
  • 8,677
  • 8
  • 51
  • 54
3
votes
2 answers

Rails 3 custom formatted validation errors?

With this model: validates_presence_of :email, :message => "We need your email address" as a rather contrived example. The error comes out as: Email We need your email address How can I provide the format myself? I looked at the source code of…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
3
votes
0 answers

Working with ActiveRecord and columns with apostrophes

I have a database I cannot modify, and it contains column names that are... let's just say stupid. Most are fine, but a couple of them have an apostrophe in it. One is called "Spouse's Birthday". I can connect to the DB fine using…
Orlando
  • 1,236
  • 3
  • 12
  • 24
3
votes
1 answer

Active Model Serializer use no serializer

At times I'd like to use no serializer for a model, and other times I do. I have tried to request nil serializer, but it seems that a serializer is used regardless. class API::FinancialDashboardSerializer < ActiveModel::Serializer attributes…
3
votes
1 answer

Rails add foreign key on non-primary key

In Rails 5.1 I have the following relations: class RootArea < ApplicationRecord has_many :common_areas end class CommonArea < ApplicationRecord belongs_to :root_area, foreign_key: 'area_id', optional: true end and below are their…
stratis
  • 7,750
  • 13
  • 53
  • 94
3
votes
1 answer

How to update Rails Controller to return an error when Model.create errors?

I have the following controller: class Api::V1::FeedbacksController < ApplicationController before_action :authenticate_user! def create @feedback = current_user.feedbacks.create( feedback_type: params[:selectedType], message:…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
3
votes
1 answer

Rails ActiveModel Serializer : Retrieving Deeply Nested ActiveRecord Association

I'm using ActiveModel::Serializer to serialize my json data. I have three models as follows class Invoice < ApplicationRecord has_many :invoiceDetails, inverse_of: :invoice belongs_to :customer accepts_nested_attributes_for…
3
votes
2 answers

Remove before_validation callback in rails using class_eval

I have a class in my model written inside a vendor's code(which I cannot modify) which has a Proc attached to it. Considering an example for a shirt class, the code looks like the one below. class Shirt < ActiveRecord::Base before_validation -> {…
3
votes
1 answer

How to handle hash nested keys validation in Rails?

I currently have a model in which I want to use dot notation to add errors to be more specific about which key in the hash attribute is faulty: class MyFormObject include ActiveModel::Validations include ActiveModel::AttributeAssignment …
Felix D.
  • 2,180
  • 1
  • 23
  • 37
3
votes
3 answers

Handle date in simpleform with activemodel in rails

I have a ActiveModel in my model like below: class PromotionCodesGenerator include ActiveModel::Model include ActiveModel::Validations attr_accessor :no_of_codes,:start_date,:end_date end And the view like below <%= simple_nested_form_for…
Fandy Dharmawan
  • 328
  • 4
  • 13
3
votes
1 answer

Selecting one of a has_many relation in Rails

What is the preferred way of selecting a specific record out of a has_many relation for a specific model in Rails? (I'm using Rails 5.) I have a model User and a model Picture which are related via the following code: class User < ApplicationRecord …
3
votes
1 answer

Rails 3 - How do I define ActiveModel translations for several attributes?

I'm trying to an activemodel instance with translations. I find that the only way validations work (with another locale) is by duplicating the error message for every field I defined int he model. So for this model: require 'active_model' class User…
sa125
  • 28,121
  • 38
  • 111
  • 153
3
votes
2 answers

Is this Rails validation thread-safe

This is running in multiple Sidekiq instances and workers at the same time and it seems that is has generated a couple of issues, like instances getting assigned the "It was alerted recently" error when shouldn't and the opposite. It is rare, but it…
3
votes
3 answers

Rails custom validation involving current_user

I have been developing a rails app in which we allow different users to submit different values for an attribute of a model. For example all users can submit values greater than 10 while administrators are allowed to submit one-digit values as…
Pedram
  • 224
  • 1
  • 3
  • 12