activemodel

Getting types of the attributes in an ActiveRecord object

I would like to know if it is possible to get the types (as known by AR - eg in the migration script and database) programmatically (I know the data exists in there somewhere). For example, I can deal with all the attribute names: ar.attribute_names.each { |name| puts name } .attributes just returns a mapping of the names to thei...

activemodel for rails < 3

Does anyone know if activemodel works with 2.3.5? I'm looking for this exact functionality (namely, validations for non-AR objects) and I'm trying to find a clean solution for a Rails 2.3.5 app. Or if anyone knows of a good gem/plugin to use that can mimic activerecord like validations for non AR objects, I'm all ears ...

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 message. For example the correct lookup for the class Conversation would be activemodel: er...

ruby one-liner for this possible?

Any chance the 2nd and 3rd lines can be combined in an one-liner and hopefully save one valuable? def self.date_format record = find_by_key('strftime') record ? record.value : "%Y-%b-%d' end the above function in a Config model try to fetch a database record by a key, return a default if not found in database. Even better if can...

How do I do a join in ActiveRecord after records have been returned?

I am using ActiveRecord in Rails 3 to pull data from two different tables in two different databases. These databases can not join on each other, but I have the need to do a simple join after-the-fact. I would like to preserve the relation so that I can chain it down the line. here is a simplified version of what I am doing browsers ...

How to set ActiveModel::Base.include_root_in_json to false?

I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's "to_json" method, and by default that method includes the root object in the JSON (which I don't want). I've tried putting this in an initializer: ActiveModel::Base.include_root_in_json = false But get the error uninitialized constant ActiveModel::Base ...

Can we connect few models of a Rails 3 app to different type of database using the ActiveRecord adapter?

Let there is a rails 3 app with two models, a Project and Notification using the ActiveRecord adapter with MySql. Project has various states using state_machine and when its state changes, I want the notifications to be stored and retrieved back using MongoDb? Can this be done using rails3? ...

Creating Join Tables for has_many & belongs_to Associations

Hello, Rails 3 newbie here... I'm working to create a devise auth system that like (yammer) has instances where users belong. I have two tables Users (email, password...) belongs_to :instance Instance (domain name, active....) has_many :users I added the belongs_to and has_many to the models but the schema hasn't been updated to a...

ActiveModel and path helpers

In a Rails3 app, I want to use resourceful paths for an ActiveModel EntityImage representing image file uploads. In some directory I have these files dir/#{type}/#{id}/#{size}.jpg (which are basically all important fields of that class) Now, probably, because 'id' is a bad name rails wise (it is not domain wise), when I want to make a d...

Disabling ActiveModel callbacks

I published an article on disabling ActiveModel callbacks, but I’m not completely sure this is the prettiest way to do something like this. Mongoid::Timestamps adds a before save callback that updates the updated_at field. Let's say I don't want that in some cases and I disable the callback like this: class User # I'm using Mongoid, ...

Rails 3: Custom error message in validation

I don't understand why the following is not working in Rails 3. I'm getting "undefined local variable or method `custom_message'" error. validates :to_email, :email_format => { :message => custom_message } def custom_message self.to_name + "'s email is not valid" end I also tried using :message => :custom_message instead as was sug...

Where are Default Validation Error Messages in Rails 3.0?

Where are the default validation error messages in Rails 3.0? What is the equivalent of ActiveRecord::Error.default_error_messages[:taken], for example? I have gotten as far as finding that ActiveModel handles the errors rather than ActiveRecord, but I can't find the errors themselves. ...

How to localize ActiveModel error messages in Rails 3?

class User include ActiveModel::Validations validates_presense_of :first_name validates_length_of :last_name, :in => 3..20, :too_long => "pick a shorter last name", :too_short => "pick a longer last name" attr_accessor :first_name, :last_name end How do you localize error message(s) for :first_name localize error message(s) ...

Mongo Design question with Rails/Mongoid for a bill tracking app

I'm writing a quick app for a user to track their daily bills (for money tracking purposes). I want the user to be able to define their own categories that a bill can be applicable for. I'm trying however to decide the best way to model this and also validate categories as unique. My initial thought was this: class User include Mon...

A way to silence "warning: Object#type is deprecated; use Object#class"

I have an application that interacts with ActiveResource from a system I have no control of. It happens that the system sends me a JSON feed and one of the fields is called "type" and, everytime this model is serialized, I get this nasty exception. Since this is a CLI application, it's very annoying. Is there a way to silence this war...

Rails 3 - How do I define ActiveModel translations for several attributes?

Hi, I'm trying to an activemodel instance with translations. I find that the only way validations work (with another locale) is by duplicating the error message for every field I defined int he model. So for this model: require 'active_model' class User include ActiveModel::Validations attr_accessor :first_name, :last_name, :email...

ActiveModel based class does not create the same results as an ActiveRecord equivilent

Hi, I am developing a Rails 3 app in a largely tabless capacity. I am using savon_model and ActiveModel to generate similar behaviour to ActiveRecord equivalents. Below is my code: class TestClass include Savon::Model include ActiveModel::Validations # Configuration endpoint "http://localhost:8080/app/TestService" namespac...

Singleton virtual model (Rails 3)

Hi, I would like to write a singleton virtual model (using Rails 3). Here is my code: class App extend ActiveModel::Naming include ActiveRecord::Associations include ActiveRecord::Reflection has_many :users def self.name 'MyApp' end end But that's not enough. If for instance I try this in Rails console: > App.firs...

get validations from model

How cat I get list of validations defined in model Example: class ModelName validates_presence_of :field_name validates_inclusion_of :sex, :in => %w(M F) end I need Hash like: {:field_name => 'required', :sex => 'Must be in: M, F'} ...