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

Rails STI: Forbidding parent class initialization

I have a rails model Book, with STI-inherited models Fiction and NonFiction While book holds a lot of common logic, I'd like to forbid creation of the parent Book model. Just wondering about the most elegant method for doing that in Rails - any…
PlankTon
  • 12,443
  • 16
  • 84
  • 153
0
votes
1 answer

Retrieve subset of many-to-many relationship in Rails/ActiveModel

As an example, I have Doctors with many Appointments with Patients. I want to retrieve all Patients who are currently active in an Appointment: class Doctor < ActiveRecord::Base attr_accessible :name has_many :appointments has_many :patients,…
at.
  • 50,922
  • 104
  • 292
  • 461
0
votes
1 answer

ActiveRecord associations outside Rails

I am using active_record to connect to a database. I am working in plain ruby and not rails. I can connect to the database fine and do all the stuff except associations. Can I do something like this? class Person < ActiveRecord::Base …
Rahul
  • 321
  • 2
  • 14
0
votes
1 answer

ActiveModel dynamic attributes

Sorry if my question is stupid, but I spent lot of time searching solution and I didn't find. I'd like to create an ApiOutputsHandler model without database. So I've created an ActiveModel. This model will be used for custom responses of my API such…
0
votes
2 answers

Changing respond_to url for an ActiveModel object

Summary: How do I customize the path that respond_to generates for an ActiveModel object? Update: I'm looking for a hook, method override, or configuration change to accomplish this, not a workaround. (A workaround is easy but not elegant.) Context…
David J.
  • 31,569
  • 22
  • 122
  • 174
0
votes
2 answers

ActiveRecord / ActiveModel Attribute Case-Sensitivity

I'm moving a legacy app from MS-SQL to Postgres which uses Rails to access the data. The columns in MS-SQL are capitalised, and while using activerecord-sql-server-adapter, they are read like this: var something = my_model.SomeAttribute Even though…
arrtchiu
  • 1,076
  • 1
  • 10
  • 23
0
votes
1 answer

Always capitalize attribute?

I have the following class: class Message extend ActiveModel::Naming include ActiveModel::Conversion attr_accessor :name, :emails, :custom_content def initialize(attrs = {}) attrs.each do |k, v| self.send "#{k}=", v end …
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
0
votes
2 answers

Is there an ActiveModel validation for association consistency

By example: A Place belongs to a User. An Event belongs to a User. An Event belongs to (but is not required to have) a Place. I'd like to validate that an Event's Place always has the same User as the Place itself. Validations of this form seem…
davetapley
  • 17,000
  • 12
  • 60
  • 86
0
votes
1 answer

Understanding how ActiveRecord exists? works when building an association

I have two models that are associated via a has_many relationship. E.g. class Newspaper < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :newspaper validates :uid, presence: true, …
Phil Ostler
  • 429
  • 3
  • 14
0
votes
2 answers

Validating presence of attributes throwing error even when the field is not blank

So I have been working on a multi-step form in ruby on rails (1.9.3) using Active model, following both the railscast #217 and #219 and am having problems with the validation the problem is that even if the attribute field is not blank it will still…
Jesse Whitham
  • 824
  • 9
  • 32
0
votes
1 answer

Active Model Validations rails 3

I have a message model and i have been looking at various gems/js for client side validation. I then started reading about Active Model Validations, im pretty new to rails so please forgive me for not totally understanding the documentation. Firstly…
Richlewis
  • 15,070
  • 37
  • 122
  • 283
0
votes
1 answer

ActiveModel validation in separate File

Is there a way to fulfil the validation with ActiveModel in a separate file? There is the possibility of Custom Validations class MyValidator < ActiveModel::Validator … end and call them with:validates_with MyValidator But the default validation…
0
votes
1 answer

How to make a field unique by the result of a coalesce operation?

class Person validates :full_name, presence: true validates :display_name, unique: ??? attr_acessible :full_name, :display_name end Full names can not be unique, but the display name must be unique. If I want to have the full_name as a…
michelpm
  • 1,795
  • 2
  • 18
  • 31
0
votes
2 answers

Rails find_by_email multiple tables

I am working on a rails application and i have 3 different user types. These users are potentially very different, so i created models for each of them. Now, they should be able to login thru a single form. So basically i want to say something like…
Kasper
  • 227
  • 4
  • 14
0
votes
1 answer

JSON Performance in Rails

I'm running a query on my index action in a Rails app, and if you look at the logs it's pretty fast: Started GET "/apartments?within=-74.01798248291016%2C40.746346606483826%2C-73.8943862915039%2C40.79314877043915&per_page=500" for 127.0.0.1 at…
Avishai
  • 4,512
  • 4
  • 41
  • 67