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

Rails 4 Change the standard Active Model Validation messages

http://api.rubyonrails.org/classes/ActiveModel/Validations.html How would you change the validation message? If I wanted to make it give back a pirate message. Like this: validates_presence_of :blank (“can’t be blank”) would instead be:…
jendiamond
  • 69
  • 7
2
votes
1 answer

ActiveModel: Validate Value for Equality

I have an ActiveModel class whose instances should only be valid if they have been touched. The following code works: class Base include ActiveModel::Model validates :touched?, inclusion: { in: [true] } def update(params = {}) …
schmijos
  • 8,114
  • 3
  • 50
  • 58
2
votes
2 answers

Rails: updating joined Active Model's attribute

I'm new to RoR. My question is about updating associated Active Model's attr. class User has_many :toys end class Toy belongs_to :user end and I have a page with a form of user where I can update user's attributes, and also certain attributes…
kong
  • 423
  • 1
  • 4
  • 12
2
votes
1 answer

Custom validator with validation helpers in it

I've defined a class which is getting fat because of many validations defined in it. So, I created a custom validator which includes all validations specific to a given context, and it's working fine. But the issue is that, while validaing any…
Yogesh Khater
  • 1,690
  • 1
  • 15
  • 20
2
votes
2 answers

validate a form field depending on radio button value

I not using ActiveRecords rather using ActiveModel to validate form data. I am stuck into some point where i needed to validate a form field depending on a radio button value. My model is class Payment include ActiveModel::Model …
sukanta
  • 541
  • 6
  • 18
2
votes
1 answer

Custom ActiveModel Validator Ruby

I would like to write a custom validator for a given validates call: class Worker include ActiveModel::Validations def initialize(graph_object) @graph_object = graph_object end attr_accessor :graph_object validates :graph_object,…
Kocur4d
  • 6,701
  • 8
  • 35
  • 53
2
votes
2 answers

Rails: Custom validations using ActiveModel

I am currently trying to make custom validations work with an input of dates, but, unfortunately, it doesn't seem to work. There are two pages inside the application, Index page and Search page. Inside the index page there is a text field that takes…
Tim
  • 1,326
  • 1
  • 15
  • 27
2
votes
1 answer

How to validate that an array is not empty in Mongoid?

class Foo include Mongoid::Document field :bars, type:Array end How to validate that the bars array is not empty?
B Seven
  • 44,484
  • 66
  • 240
  • 385
2
votes
1 answer

How to apply ActiveModel::Serializer to a custom object?

If I specify simple array of ActiveModel objects serializer works: format.json { render json: @childs, each_serializer: ItemSerializer } But I need to respond with JSON with additional fields, such as parent_id, etc. {parent_id: 15, childs:…
2
votes
1 answer

Customise ActiveModel full_messages

I would like to remove the attribute from my custom validation messages and just display the message, so instead of School Please Provide Your School Name I want to return Please Provide Your School Name As set in my model here validates :school,…
Richlewis
  • 15,070
  • 37
  • 122
  • 283
2
votes
0 answers

Incorrect validation context in nested resource

I'm having a problem with a form in Rails 4.1.8. It contains a few attributes from the Pledge model and a few more from a has_one association called Grant. In the controller, @pledge is assigned the params and validated with a custom…
svoop
  • 3,318
  • 1
  • 23
  • 41
2
votes
2 answers

Rails add to model on the fly

I have a model Agent that belongs_to Scenario. Both models have the field options and I would like to merge the values stored in Scenario options with Agent options so that I can do @agent.options and retrieve the values from both Agent and…
Adam
  • 673
  • 1
  • 8
  • 18
2
votes
1 answer

Rails app site wide settings editable from the front end

I have a small Rails 4.2 app, that has a few settings that I would like to be changed site wide e.g start_time: time, finish_time: time, disabled: boolean Im aware I could make a model called AppSettings and have those fields in it but I would only…
Sam Mason
  • 1,037
  • 1
  • 8
  • 29
2
votes
1 answer

How to create indexes with elasticsearch-model, bulk index doesn't work

I use elasticsearch-model gem to integrate elasticsearch with activerecord, I am using this module: #http://www.codinginthecrease.com/news_article/show/409843?referrer_id= module PostImport def self.import Post.find_in_batches do |posts| …
user3995789
  • 3,452
  • 1
  • 19
  • 35
2
votes
1 answer

Enums in Rails without ActiveRecord

I want to use enums with MongoMapper and rails 4. I know there are gems out there (enumerize) which adds this behaviour but it seems like a lot to add for a small feature. What is the easiest way of creating an enum for an ActiveModel class?
caffeinated.tech
  • 6,428
  • 1
  • 21
  • 40