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
3 answers

Casting ActiveModel attribute on return

In my Rails 3.1.1 project I have an ActiveModel that talks to API (ripped from Paul Dix's book, shortened for readability): class Job include ActiveModel::Validations include ActiveModel::Serializers::JSON ATTRIBUTES = [ :id, …
1
vote
2 answers

Creating several objects with relationships in rails

I'm new to rails. I have a signup form, where the user can create a project, at the same time, as signing up. The project should get created, and the new user is made "Admin" of the project. I have the following models: class Project < A::B …
Jo Erlang
  • 327
  • 7
  • 15
1
vote
2 answers

Rails model variable changing unexpectedly

Going mad here. Any pointers gratefully received! I've got a Delivery model and I'm trying to add a method to update the delivery state, based on delivery lines. This function is defined within the model class, and delivery_state is one of the…
asc99c
  • 3,815
  • 3
  • 31
  • 54
1
vote
3 answers

Check for changes that override default on newly created record in Rails 7

For our application I am newly creating records in two ways: With the default values in the database. Manually setting attributes. I need to distinguish between these changes so that I know whether the attribute is manually overridden, or whether…
Yan
  • 23
  • 5
1
vote
1 answer

Rails - models with same class name, but one is inside a module

I'm adding a model to my project which shares a name with a class in the models directory, but it's in a module and has no relation (figuratively and literally) to the existing model: Offer and Partner::Offer. + app |-+ models |-- offer.rb |-+…
Argus9
  • 1,863
  • 4
  • 26
  • 40
1
vote
1 answer

Trying to add multiple entry to a particar database active records on rails with a single post request

i have an array of selected product_ids and product quantities i need to insert through active records into the rails database model in a single HTTP request. the orderitem arrays belong to an order_detail active record model. I need to be able to…
1
vote
1 answer

Mix create and update validations in single custom validator using ActiveModel::Validator

I found two alternatives to make use of the ActiveModel::Validator helper, but I am failing to find an easier or clean(er) approach than either checking for the presence of record.id or creating multiple custom validators. I have a User class as…
chriszo111
  • 343
  • 5
  • 17
1
vote
1 answer

Rails 3: Checking if nested attributes have been changed?

Just wondering if there's a method in rails to identify if nested attributes have been changed when a form is saved? Eg: I have a model called 'shop', which has nested check boxes for delivery areas. Each time 'shop' is changed, I'd like to know if…
PlankTon
  • 12,443
  • 16
  • 84
  • 153
1
vote
2 answers

Why is numericality validator not working with Active Model Attributes?

I'm using Rails 7 and Ruby 3.1, and Shoulda Matchers for tests, but not Active Record, for I do not need a database. I want to validate numericality. However, validations do not work. It looks like input is transformed into integer, instead of being…
1
vote
1 answer

Using form_with with an ActiveModel object?

I have an object that uses the ActiveModel::Model concepts: class TransitProvider include ActiveModel::Model # ... more stuff Underneath the covers, this object is an aggregate for a Provider record and a Service record. Everything seems to be…
Dan Sharp
  • 1,209
  • 2
  • 12
  • 31
1
vote
1 answer

Rails 7 - ActiveModel date validation | Is there a replacement for validates_timeliness gem?

While trying to upgrade my application to Rails 7, I came across the following deprecation warning : ActiveRecord::Base.default_timezone is deprecated and will be removed in Rails 7.1. Use ActiveRecord.default_timezone instead. On further…
1
vote
1 answer

Why are these Rails validations different?

validates :password, :presence => { :on => :create }, :length => { :within => 4..40 } and validates :password, :presence => { :on => :create }, :length => { :within => 4..40, :on => :save } I thought the default for a…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
1
vote
2 answers

ActiveRecord relations: Can A has_many Bs AND A has_one B at same time?

I have a situation that I'm not sure how to handle in Rails: Event has_many :photos and Photo belongs_to :event simple enough But, Event also needs to reference a single "key" photo. Thought about adding: Event has_one :key_photo, :foreign_key =>…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
1
vote
2 answers

In Rails 4, how do I reference an enum value when initializing my model?

I’m using Rails 4.2. It is not an option to upgrade at this time. I have this model class OrderItem < ApplicationRecord … enum type: { data: “Data”, product: “Product” } How do I initialize my object by referencing my enum? I tried…
Dave
  • 15,639
  • 133
  • 442
  • 830
1
vote
1 answer

Validate existing records with overlap item ranges taking nil as infinite

I'm here because I've been looking around for some queries that could help to find existing records with overlapping item ranges but I couldn't find anything. Let's say I have a model named Cart. A Cart has the following attributes: item_min and…