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

Rails: set a model attribute as read-only, but allow updating via method

I have an accounts model, where I would like the balance to be readonly, but it can be updated via private methods. Currently class Account < ActiveRecord::Base def deposit(amount) # do some stuff and then update_balance(amount) end …
Amin Shah Gilani
  • 8,675
  • 5
  • 37
  • 79
3
votes
2 answers

ActiveModel::Serializer belongs_to attribute is not being displayed in json response

I'm not quite sure why this isn't working but I have the following serializer: class ExternalAccountSerializer < ActiveModel::Serializer attributes :id, :account_name, :type belongs_to :user, serializer: UserSerializer end The API is returning…
3
votes
1 answer

Overriding validations in Rails Single Table Inhertiance models

Let's say I have two models using Rails Single Table Inheritance. I can easily add validations in child model to make certain field required. But what if I want change validations making fields optional in Child model or have different criteria…
Dmitry Polyakovsky
  • 1,535
  • 11
  • 31
3
votes
1 answer

How to make a plain ruby object assignable as active record association

I have an Audit class which is backed by ActiveRecord. class Audit < ActiveRecord::Base belongs_to :user, polymorphic: true end I have a User class which is a plain ruby object with some ActiveModel functionality included. It's not a database…
David Tuite
  • 22,258
  • 25
  • 106
  • 176
3
votes
1 answer

validations on pure ruby class

I have two classes; customer and reservation. And my project is consist of only ruby code, not rails project. Class reservation reads bulk json file line by line which includes customer hash. From that hash, I create customer object. Here's the code…
CanCeylan
  • 2,890
  • 8
  • 41
  • 51
3
votes
1 answer

DirtyAttributes take as changed BigDecimal types

I'm in a model callback (after_save) and one of the attributes is BigDecimal type. So when I change another attribute and check dirty attributes with changes method I have this: {"amount"=>[#,…
AlexLarra
  • 841
  • 5
  • 18
3
votes
1 answer

How to use ActiveModel::Lint::Tests in Rspec

For default Rails, you can add so called ActiveModel::Lint::Tests to see if your models adhere to parts of the ActiveModel. I would like to call these, or an equivalent thereof in my Rspec tests. I don't want to test the exact behaviour: the lint…
berkes
  • 26,996
  • 27
  • 115
  • 206
3
votes
1 answer

Rails 3 ActiveModel Nested Class I18n

Given the following class definition in ruby: class Conversation class Message include ActiveModel::Validations attr_accessor :quantity validates :quantity, :presence => true end end How can you use i18n to customize to error…
3
votes
0 answers

Is it possible to both embed and include associated objects with ActiveModel::Serializer?

We are in a weird situation where we have a relied-upon endpoint that both embeds data and sideloads it. I have no idea why it was built this way, and why no one thought hey, we seem to be sending all of this data twice, but to divine the reasons of…
3
votes
1 answer

How Can I Get An Enum's String Value Dynamically?

I have a Video model with a property called aspect_ratio that is an enum: ASPECT_RATIOS = [:four_three, :sixteen_nine] enum aspect_ratio: ASPECT_RATIOS The property is stored as an integer. When I access this property, I get a string…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
3
votes
3 answers

ActiveModel validate custom setter

Say I have a custom setter on an activemodel model DateRange, to automatically set a DateTime attribute if a string is entered. It looks like this def from=(value) @from = value.to_date end Now this does work if you enter a valid date…
Marco Prins
  • 7,189
  • 11
  • 41
  • 76
3
votes
3 answers

ActiveModel::ForbiddenAttributesError + cancan + rails 4 + model with scoped controller

I m using cancan(1.6.10) with rails 4.0.0. I have a model called 'App'(not scoped) and a controller Admin::AppsController(its scoped. ie app/controllers/admin/apps_controller). the controller code is as class Admin::AppsController <…
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
3
votes
1 answer

How do I add a validation to a singleton_class only?

When I add a validation to a singleton_class it seems to get assigned to the base class instead and it doesn't trigger for either. class Example attr_accessor :title, :some_boolean include ActiveModel::Validations end puts Example.validators …
Shawn Balestracci
  • 7,380
  • 1
  • 34
  • 52
3
votes
2 answers

How to access translation key for ActiveModel validation error?

I Have a situation, where I wan't to store the translation key for a validation error in my db instead of the error message it self. Imagine the following situation: class Car < ActiveRecord::Base validates_presence_of :year, :fuel end car =…
Niels Kristian
  • 8,661
  • 11
  • 59
  • 117
3
votes
1 answer

why don't rails tableless models include associations?

After looking at a few tableless solutions in Rails (virtus, active_attr, activemodel) it's clear that Rails associations are not supported. My question is why not? Is there some obvious reason for this that I'm missing? Seems like associations…
Dty
  • 12,253
  • 6
  • 43
  • 61