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

How to carry over form field values after an invalid submit, using only ActiveModel?

It appears to be normal functionality for a Rails form that has been submitted with invalid data to display error messages and repopulate the form with the invalid data. At least, all my Active Record managed resources work this way. However, I…
3
votes
1 answer

idiomatic way to do regular expression searches in rails models?

in my rails controller, i would like to do a regular expression search of my model. my googling seemed to indicate that i would have to write something like: Model.find( :all, :condition => ["field REGEXP '?' " , regex_str] ) which is rather nasty…
yee379
  • 6,498
  • 10
  • 56
  • 101
3
votes
2 answers

Rails: overriding model name in params

I have the following model: class Action < ActiveRecord::Base # model code stuff end In the controller, I can do something like that: class ActionsController < ApplicationController # super simple update def update @action =…
alexs333
  • 12,143
  • 11
  • 51
  • 89
3
votes
1 answer

Rails where to place your Activemodel::validators

This is a two part question. Part one if my Model that i am writing the validations for is inhering from ActiveRecord::Base do i need to include ActiveModel::Validations within that class?? the API for rails doesnt say but here in yehudakatz blog…
legendary_rob
  • 12,792
  • 11
  • 56
  • 102
3
votes
2 answers

Accessing attributes from different associated models rails 3

I am looking to get a better understanding of Active Model/Record relationships and how to call attributes dependent upon where the attributes are located (models) and where I am calling them. So for example I can access the attribute dish_name from…
Richlewis
  • 15,070
  • 37
  • 122
  • 283
3
votes
3 answers

Ruby on Rails Activemodel with Associations

Acutally i face some hard exercises in computer science (hard for me i think, haha). We're doing some basic stuff with Ruby on Rails an i have to open a csv file to get additional information on my 'User' model which is a normal rails scaffold. So…
melanie93
  • 31
  • 2
3
votes
1 answer

How to get records from multiple condition from a same column through associated table

Let say a book model HABTM categories, for an example book A has categories "CA" & "CB". How can i retrieve book A if I query using "CA" & "CB" only. I know about the .where("category_id in (1,2)") but it uses OR operation. I need something like AND…
Yakob Ubaidi
  • 1,846
  • 2
  • 20
  • 23
3
votes
1 answer

Restrict eagerly loaded with `where` without another query in Rails

A has many Bs, B has many Cs. C has a property called thing: class A < ActiveRecord::Base has_many :bs end class B < ActiveRecord::Base belongs_to :a has_many :cs end class C < ActiveRecord::Base belongs_to :b attr_accessible…
Asherah
  • 18,948
  • 5
  • 53
  • 72
3
votes
3 answers

How to define virtual attribute for date_select drop menu using active model

I am using active model for payment process in my application , but i am not able to add field for credit card expiration date, since it throws error as below undefined method `card_expiration_date(3i)='. In my model: class CartServer include…
loganathan
  • 5,838
  • 8
  • 33
  • 50
3
votes
2 answers

Rails Association Validations: The field, or the _id field?

One of the messier practices I have in Rails development is juggling validations of associated fields between validating the actual object (eg: validates_presence_of :related_object) and validating on the id column for that association (eg:…
PlankTon
  • 12,443
  • 16
  • 84
  • 153
2
votes
1 answer

Why not refer to the session hash in the Model layer?

This question has been repeatedly asked and answered with one-line assertions, such as "because it is an obvious violation of MVC." Frankly, I just don't get it. Indeed, it feels to me that putting session inside the controller merely an artifact…
2
votes
1 answer

Mapping a JSON Api to a Model in Rails

I have access to a JSON API and would like to map the API to some classes in my Rails 3.2.1 model, thus I don't need any datatabse. Example : The API returns the current user with the following JSON {"first_name":"John","last_name":"Smith"} I'd…
geoffroy
  • 575
  • 6
  • 18
2
votes
1 answer

Rails 3 and Mongoid: Embedded documents validation

So, I am having some issues with user authentication in embedded documents. I have two documents, one embedded in the other. A business has many members. The models look like this: class Member include Mongoid::Document field :username, …
Red
  • 2,256
  • 5
  • 25
  • 38
2
votes
1 answer

How to restore the class of associations from an ActiveModel object serialized into JSON?

I have a class including ActiveModel that needs to have some "associations", like this: a = ActiveModelClass.new a.user = User.find(1) I'm just using an attr_accessor for this: attr_accessor :user ### Elsewhere ### a.user.name # =>…
Kevin Griffin
  • 14,084
  • 7
  • 28
  • 23
2
votes
1 answer

RSpec and ActiveModel

I have a module and it includes activemodel and I want to test it using rspec. This is my setup so far: lib/ |__ my_module/ | |__ base.rb |__ my_module.rb spec/ |__ my_module_spec.rb |__ spec_helper.rb |__ support/ |__…
Cecille Manalang
  • 213
  • 3
  • 14