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

Rails - Help understanding how to use :dependent => :destroy

I have the following models: User (id) Project (id) Permission (project_id, user_id) Thread (project_id) ThreadParticipation (thread_id, user_id) So that's working nicely, problem is this. When a user leaves or is removed from a project, I need all…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
2
votes
1 answer

In Rails - How to have one query that has multiple queries?

In have 3 models here: projects threads (project_id) thread_participations (thread_id, read boolean) Right now I have a list of the user's projects, and the list shows how many threads are unread per project. The huge problem here is that if the…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
2
votes
1 answer

Getting a undefined method `validates_presence_of' for for a custom attr_accessor

I think the trouble is coming from the fact this is a custom Model. Error: undefined method `validates_presence_of' for Calculation:Class My Model: class Calculation extend ActiveModel::Naming include ActiveModel::Conversion def persisted? …
Trip
  • 26,756
  • 46
  • 158
  • 277
2
votes
1 answer

Rails: how to generate locale files for validations in my models?

I have an existing codebase and I want to support internationalization for all the validations on the models. It should be as easy as adding the appropriate lines in each locale file like es.yml and de.yml in the format of activerecord: …
2
votes
1 answer

Given a Rails 5 has_and_belongs_to_many, how to create a join record?

I have the following schema: class Industry << ApplicationRecord has_and_belongs_to_many :departments end # Relationship table: departments_industries class Department << ApplicationRecord has_and_belongs_to_many :industries …
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
2
votes
6 answers

Validation is not working if integer value of old attribute is same as that of new attribute

I've a model Cart having has_many relationship with cart_items. # cart.rb: accepts_nested_attributes_for :cart_items, allow_destroy: true has_many :cart_items, dependent: :destroy, inverse_of: :cart # cart_item.rb: validates :quantity,…
Radix
  • 2,527
  • 1
  • 19
  • 43
2
votes
1 answer

Rails 4 - Validates uniqueness with has_many through

I have these three models: User: class User < ActiveRecord::Base validates :name, presence: true validates :surname, presence: true validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i } has_many…
Mattia M.
  • 464
  • 1
  • 4
  • 16
2
votes
2 answers

Is is possible to return virtual attributes automatically in Rails models?

Given: class Foo has_one :bar def bar_name bar.name end end class Bar belongs_to :foo end In the console or in a view, I can @foo.bar_name to get 'baz'. I'm aware that I can @foo.as_json(methods: :bar_name) to get {"id"=>"abc123",…
Micah Alcorn
  • 2,363
  • 2
  • 22
  • 45
2
votes
1 answer

Ruby on Rails: ActiveRecord-like queries on non-persisted objects?

In my project, a large number of POROs are composed from various data sources such as external APIs. The objects looks like: {id: 1, name: 'Peter', age: 8}, {id: 2, name: 'Jack', age: 4}, {id: 3, name: 'Tom', age: 12} I would like to have an…
mye
  • 103
  • 2
  • 6
2
votes
2 answers

Dirty Tracking of embedded document on the parent doc in Mongoid

I had to track the dirty objects. And it works fine with the parent doc. But when I change the embedded or referenced in doc, the dirty has to be accessed via the embedded/referenced in doc itself. How can I track the dirty on the parent doc…
millisami
  • 9,931
  • 15
  • 70
  • 112
2
votes
3 answers

Rails custom model method in where query

In my rails app I have defined in the Kid model a calculation based on the fields from the Kids DB. the method is as follows: def flip_date self.dob.advance(months: 10) end I want to use this in my controller as I have a method where I am…
Alvin Lau
  • 101
  • 1
  • 11
2
votes
1 answer

Error messages in nested attribute

I've three models called Account,User and AccountPermission. I'm creating the Account via AccountPermission while creating user. However, If a problem occurs related with Account :name, the system throws something like that below. Account…
utkuDAT
  • 334
  • 1
  • 3
  • 16
2
votes
1 answer

API versioning for active models

I am building a application in which i have a model user, In the first version of the application there will be no mobile number column for the users in the later version it must be present, is there any way to add versioning to models so that…
Bala Karthik
  • 1,353
  • 2
  • 17
  • 26
2
votes
2 answers

How ActiveModel in Rails add methods to model class by just having single line "has_secure_password" in model?

In Rails (4.2.6) I can include gem 'bcrypt' in Gemfile, install it and then simply add line 'has_secure_password' in my model class. I wonder, how it works in terms of adding methods to my model class. If I'm looking into the source of…
Pavel Bulanov
  • 933
  • 6
  • 13
2
votes
2 answers

NameError in Admin::ResidentsController#destroy Rails

I am getting name error when I am deleting a resident model object using active admin I have resident model : class Resident < ActiveRecord::Base has_many :leaves,dependent: :delete_all end And second one is leave model: class Leave <…