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

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…
Russ Bradberry
  • 10,705
  • 17
  • 69
  • 85
4
votes
3 answers

Rails has_many :through association

I'm trying to create a rails app where user can create events and invite participants to it and need your help! I've been going in circle, trying few things but doesn't seem right at all and this is now driving me crazy!! I'm using rails 4. How…
4
votes
4 answers

How do I ensure a model always uses a transaction and locks (in Rails)?

I noticed that Rails can have concurrency issues with multiple servers and would like to force my model to always lock. Is this possible in Rails, similar to unique constraints to force data integrity? Or does it just require careful…
Chloe
  • 25,162
  • 40
  • 190
  • 357
4
votes
2 answers

ActiveModel::Errors translate attribute part of error message?

Looking for a solution how to write translation record in I18n yml file for the following case: class SomeClass < ActiveRecord::Base validate: stock_avail def stock_avail # errors is an instance of ActiveModel::Errors …
David Unric
  • 7,421
  • 1
  • 37
  • 65
4
votes
4 answers

How i can write a query inside select in rails

How can I write this query in Ruby on Rails? Query inside a select SELECT id, company_id, (SELECT name FROM companies WHERE id = referred_to_id) AS name FROM referrals WHERE company_id = 21
kashif
  • 1,097
  • 4
  • 17
  • 32
4
votes
2 answers

Validating Child Object with ActiveModel Validations

I have two plain Ruby classes, Account and Contact. I am using Simple Form's simple_form_for and simple_fields_for to create nested attributes. I am looking to fulfill the following validation requirements: An associated Contact must exist for…
Eric M.
  • 5,399
  • 6
  • 41
  • 67
4
votes
1 answer

Validate presence before custom validation

validates :place_id, :title, :level, :start_at, :end_at, :presence => true validate :event_takes_place_in_one_day, :event_is_not_in_past def event_takes_place_in_one_day binding.pry self.start_at.day == self.end_at end and bang! error,…
lessless
  • 866
  • 10
  • 27
4
votes
1 answer

undefined method `model_name' for ActiveModel::Errors:Class

I have the following mongoid model class: class Exercise include Mongoid::Document field :name, :type => String field :description, :type => String belongs_to :group validates_presence_of :name, :description, :group end And I have the…
dagda1
  • 26,856
  • 59
  • 237
  • 450
3
votes
2 answers

Validate date and time fields together in rails model

EDIT: In case the question below looks a bit 'extensive', the summary is that I just want to combine a date field and an optional time field in my model purely for the purpose of validating it using a date validator but I can't get my test to fail…
user1116573
  • 2,817
  • 4
  • 17
  • 27
3
votes
1 answer

Multiple Associations in a Model

I have a User model and an Account model. The user has many accounts and the accounts belong to one user. I have the models and associations all set up. Now I want to make one of those accounts the "primary account". What is the best way to set up…
jasonlfunk
  • 5,159
  • 4
  • 29
  • 39
3
votes
1 answer

Self.find method Active Model in Rails not working

I'm currently using an API and not a database, and I want to be as close as ActiveRecord, so I decided to go ahead and do exactly like this railscast here: http://railscasts.com/episodes/219-active-model So far, my save method works well, so I can…
allaire
  • 5,995
  • 3
  • 41
  • 56
3
votes
1 answer

Mass-assign exception no explanation found (Rails 3.2.1)

Please see this post as well. Note: The current rake task saves User and Topic objects, but not posts or tags when setting a post object. Question: What is a proper way to describe this relationship in my rake task (shown below) so that it does not…
rhodee
  • 1,257
  • 1
  • 14
  • 27
3
votes
1 answer

Rails ActiveModel Attribute Type

I am using ActiveModels in a rails 3.1.1 project without a database backend. I am wondering how I can set the types of attributes to String, Boolean, Decimal. From my understanding, when using ActiveRecord backed by a database, the type would be…
Martin
  • 15,820
  • 4
  • 47
  • 56
3
votes
1 answer

Rails associations can't mass-assign foreign key

Maybe I am doing it wrong but here is my issue: @restaurant = current_user.restaurants.build(params[:restaurant]) This builds a new restaurant object where the user_id is set to the current_user.id. This only works if you set attr_accessible…
3
votes
1 answer

Friendship model, how to determine if a friendship model already exists between 2 users

I have a Friend model: user_id, friend_id, status (approved, pending, ignored) Given a user currently logged in, I want to be able to do something like current_user.friendship_exists(@user) Something to tell if a friendship exists for a logged in…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012