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

Default values for model attributes in Rails

What's the proper way to set default values for models in Rails? class User < ActiveRecord::Base attr_accessible :name, :points end I want points to start out at 0 instead of nil. Ideally the default value is created right away rather than…
at.
  • 50,922
  • 104
  • 292
  • 461
2
votes
1 answer

Is it possible to use ActiveModel with Sinatra?

I want to use ActiveModel in my Sinatra models to work with mongodb's Ruby driver. Is that a straight forward exercise or will there be problems?
Roman
  • 10,309
  • 17
  • 66
  • 101
2
votes
2 answers

ActiveModel validate attribute based on association

I have at least 2 classes. One class must validate one of its attributes based on the value of an associated model's attributes. The below code is what I am going for, but its just an idea, it doesn't work. Any way to achieve it? class Concert …
chris
  • 6,653
  • 6
  • 41
  • 54
2
votes
2 answers

Neo4j::Rails::Model to_json - node id is missing

I have a Jruby on Rails application with Neo4j.rb and a model, let's say Auth, defined like this: class Auth < Neo4j::Rails::Model property :uid, :type => String, :index => :exact property :provider, :type => String, :index => :exact property…
Vlad V
  • 1,594
  • 1
  • 13
  • 28
2
votes
2 answers

ActiveModel: dangerous use of send()?

In RailsCast 219, the following code is offered for creating a class for ferrying data back and forth from forms, but without any ActiveRecord persistence: class Message include ActiveModel::Validations attr_accessor :name, :email, :content …
George Armhold
  • 30,824
  • 50
  • 153
  • 232
1
vote
1 answer

Rails accepts_nested_attributes > build being lost on validation fail

When a validation occurs whilst saving to the location model using accepts_nested_attributes for the location model, Rails will return the form blank when it previously held values. class Sale < ActiveRecord::Base belongs_to :location belongs_to…
Elliot
  • 2,199
  • 4
  • 21
  • 28
1
vote
1 answer

Rails, how to setup a foreign key to be auto assigned

Given the following models: User.rb NetworkDepartment.rb (id, title,added_by) belongs_to :user, :foreign_key => :added_by I want to be able to create a network department and have the added_by assigned automatically. I have the…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
1
vote
1 answer

How to add a User model validation to block certain email domains

I would like to have a list of email domains that are validated against to prevent from registering on my app. 10minutemail.com, yopmail.com, mail.com, mail.ru etc... I have a list of domains in my user model like so: BAD_DOMAINS =…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
1
vote
3 answers

mongoid rails mongodb data not cleaning up

Rails 3.2.1 app name: demo database: mongoDB with mongoid I have this scaffolding set up in rails 3.2.1: localhost:3000/pages, and I have these fields: title, content. I have already filled in 3 rows of data to each field. The problem is: after…
The questioner
  • 724
  • 10
  • 21
1
vote
1 answer

How to create ActiveModel Object in rails 3

I am having the following model class on ActiveRecord. How to write an equivalent ActiveModel for this class? class Recommendation < ActiveRecord::Base def self.columns() @columns ||= []; end def self.column(name, sql_type = nil, default = nil,…
Achaius
  • 5,904
  • 21
  • 65
  • 122
1
vote
1 answer

validates :something, :confirmation => true & attr_accessor confusion

am struggling with Ruby validates :confirmation => true in my Rails app. Consider the following code: # == Schema Information # # Table name: things # # id :integer not null, primary key # pin :integer(8) # created_at…
Jeff
  • 13
  • 3
1
vote
1 answer

ActiveModel include_root_in_json

I am a bit confused by this option... which can be found in the example below user = User.find(1) user.as_json # => { "user": {"id": 1, "name": "Konata Izumi", "age": 16, "created_at": "2006/08/01", "awesome": true} } …
Grofit
  • 17,693
  • 24
  • 96
  • 176
1
vote
1 answer

Rails: Database structure as models

So this may sound a bit awkward, but I would like to have the database structure (tables, fkeys, columns) as a projection on Rails models e.g. the model Table < ActiveModel which would respond to call like…
FreeCandies
  • 1,088
  • 10
  • 21
1
vote
1 answer

avoiding code duplication in Rails 3 models

I'm working on a Rails 3.1 application where there are a number of different enum-like models that are stored in the database. There is a lot of identical code in these models, as well as in the associated controllers and views. I've solved the code…
Dustin Frazier
  • 293
  • 2
  • 9
1
vote
2 answers

How to iterate over all "table columns" in an ActiveModel?

I want to iterate over all the columns in a table for a Model and perform some logic on it. def fetch_all_fields @profile_page ||= profile_page SOMETHING.each do |field_name| method_name = "remote_#{field_name}" if…
berkes
  • 26,996
  • 27
  • 115
  • 206