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

How to get name of current rake task in my Rails model?

I have some problems with one of gem supporting ActiveModel caching. When I'm using observer for cached model, during application initialization it tries to describe table to get all fields names. The same thing is done when rake task is running,…
user1425694
  • 101
  • 1
  • 4
8
votes
7 answers

rails 3.2.0 and heroku

I have some troubles with rails 3.2.0 & ruby 1.9.3, I only created a new app with rails new command and I'm trying to deploy to heroku. I'm having the following app error: An error occurred in the application and your page could not be served.…
nachoesmite
  • 81
  • 1
  • 4
8
votes
3 answers

Ruby on Rails 3 (3.1) ActiveModel Associations (tableless nested models)

How to impliment ActiveModel associations (tableless nested models)? For example: book has many chapters With ActiveRecord I would create two models and assosiate them with has_many and belongs_to. But ActiveModel doesn't have such functionality.…
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
8
votes
1 answer

Shoulda-Matchers and Custom Error Messages

I am trying to do some basic rspec testing with shoulda matchers and I've come upon an error I haven't seen asked on SO before. I have a unique attribute called name, but for reasons required of the project I have overwritten the default "has…
MageeWorld
  • 402
  • 4
  • 14
8
votes
5 answers

Detecting if value of attribute changed during last update doesnt work with Active Model Dirty

I am trying to send a notification email in my rails app only if the value of my column status was modified by the current update. I tried using Active Model Dirty as was suggested in some post and the status_changed? method. Unfortunately my email…
David Geismar
  • 3,152
  • 6
  • 41
  • 80
7
votes
2 answers

Rails accepts_nested_attributes count validation

I've got three models. Sales, items, and images. I'd like to validate that when a sale is created there are at least three photos per sale and one or more items. What would be the best way to achieve this? Sales Model: class Sale <…
Elliot
  • 2,199
  • 4
  • 21
  • 28
7
votes
1 answer

Getting ActiveModel::Callbacks to work with ActiveResource

I am trying to get ActiveModel::Callbacks to work with ActiveResource (specifically after_initialize) for a Rails 3 app, but I can't seem to get it to work. I don't get any errors, but the callback method is never executed. Here is a snippet of…
gmoniey
  • 7,975
  • 4
  • 27
  • 30
7
votes
1 answer

Rails validatation to ensure a username does not clash with an existing route?

I want to ensure users can't create usernames that clash with my existing routes. I would also like the ability to deny future routes I may define. I am thinking of accomplishing this like so: In the model: class User < ActiveRecord::Base …
7
votes
1 answer

Rubocop Uniqueness validation should be with a unique index, in values that start from some specific values

I have a rails model that validates the uniqueness of order_number value, thay should start from 1_000_000, so I added a variable that is used as a first value: # order model STARTING_NUMBER = 1_000_000 validates :order_number, uniqueness:…
Roman
  • 743
  • 10
  • 21
7
votes
1 answer

Active Model Serializers has_many associations

I'm using the gem active_model_serializers. Serializers: class ProjectGroupSerializer < ActiveModel::Serializer attributes :id, :name, :description has_many :projects end class ProjectSerializer < ActiveModel::Serializer attributes :id,…
dev_054
  • 3,448
  • 8
  • 29
  • 56
7
votes
2 answers

Validating nested models with ActiveModel::Validations

My application uses plain Ruby classes with ActiveModel::Validations, without implementing ActiveRecord: class Car include ::ActiveModel::Validations attr_accessor :engine end class Engine include ::ActiveModel::Validations attr_accessor…
David Elner
  • 5,091
  • 6
  • 33
  • 49
7
votes
3 answers

Rails Undefined Method 'model_name'

I have the following model: class Contact attr_accessor :name, :emails, :message def initialize(attrs = {}) attrs.each do |k, v| self.send "#{k}=", v end end def persisted? false end end I am calling to a contact form…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
7
votes
2 answers

Rails 3 ActiveModel: cannot include ActiveModel::Model directly

In my Rails 3.2.11 and "development" environment when I try to have an active model: class DisponibilityApi include ActiveModel::Model attr_accessor :start_time, :end_time validates :start_time, :end_time, :presence => true end I have an…
mishaker
  • 115
  • 3
  • 8
7
votes
2 answers

Change input name of model

Using the ActiveAttr: class Filter include ActiveAttr::Model attribute term # Overriding to_key, to_param, model_name, param_key etc doesn't help :( end class SpecialFilter < Filter end How do I override the ActiveModel to generate the…
Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
7
votes
2 answers

Does ActiveModel have a module that includes an "update_attributes" method?

I've set up an ActiveModel class in my Rails app like this: class MyThingy extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion attr_accessor :username,…
Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145