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

How to find and remove part of a string in Rails Console - ActiveRecord

I'm looking for a way in the rails console to find any values that finish with a particular string and remove that string from the value. Something along the lines of: Model.all.each{|x| x.duration.slice!(" weeks")}.where("duration IS NOT NULL")…
jake
  • 91
  • 2
  • 6
5
votes
6 answers

Rails where condition and order

Knowing that by default Rails orders data by ID, how can I order by ids given to the where clause? ids = Bookmark.where(user_id: 7).order(created_at: :desc).pluck(:company_id) Result: [146, 140, 128, 4, 2] Now, when I try to get the companies in…
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
5
votes
1 answer

postgres_ext/serializers help... seems so simple, but can't get anything to work as described

https://github.com/dockyard/postgres_ext-serializers this seems so simple to setup, but I can't seem to get any basic functionality to work in terms of moving the JSON from rails to postgres. I tried including the following in my ams init, in my…
avian
  • 1,693
  • 3
  • 20
  • 30
5
votes
2 answers

Rails 4 and Active Admin: ActiveModel::ForbiddenAttributesError

The full error is the following: ActiveModel::ForbiddenAttributesError in Admin::ProductsController#create My product model only has a name and price. Why is commit a parameter? When I click the 'Create Product' button within the admin dashboard,…
Nubtacular
  • 1,367
  • 2
  • 18
  • 38
5
votes
1 answer

Rails - Polymorphic Favorites (user can favorite different models)

We are trying to add multiple favoritable objects, where a user can favorite many different objects, but are not sure how to make it work. Here is the Favorite model: class Favorite < ActiveRecord::Base # belongs_to :imageable, polymorphic: true …
5
votes
4 answers

How does one put validations on individual ActiveModel/ActiveRecord objects?

You have a model, say, Car. Some validations apply to every Car instance, all the time: class Car include ActiveModel::Model validates :engine, :presence => true validates :vin, :presence => true end But some validations are only relevant…
John Feminella
  • 303,634
  • 46
  • 339
  • 357
5
votes
3 answers

RoR: first_or_initialize block doesn't save

I have the following code, which works fine with no errors but the models never get saved... myarray.each do |item| r = MyModel.unscoped.where(:site_id => @site.id, :url => item['permalink_url']).first_or_initialize do |r| r.title …
Ashley
  • 5,939
  • 9
  • 39
  • 82
5
votes
2 answers

Devise not working with Rails 4.0rc1

Is there a way to get Devise to work with Rails 4.0rc1? I get the following error when trying to start the rails server or even to generate the Devise…
at.
  • 50,922
  • 104
  • 292
  • 461
5
votes
4 answers

How to validate in a model, data from a controller

So I have some data that gets pulled from another rails app in a controller lets call it ExampleController and I want to validate it as being there in my model before allowing the wizard to move to its next step and I can't quite figure out how I…
4
votes
1 answer

ActiveModel dynamic attribute type

Is it possible to decide which type casts an attribute in ActiveModel::Attributes on runtime? I have the following code class DynamicType < ActiveModel::Type::Value def cast(value) value # here I don't have access to the underlying instance of…
23tux
  • 14,104
  • 15
  • 88
  • 187
4
votes
1 answer

ActiveModel equivalent for ActiveRecord has_attribute?

I am using ActiveModel because I am hooking up to a third party API rather than a db. I have written my own initialiser so that I can pass in a hash and this be converted to attributes on the model - to support some of the forms in the…
GrahamJRoy
  • 1,603
  • 5
  • 26
  • 56
4
votes
2 answers

How to pass an empty text array from view to controller for update in Rails?

I have a Model that has a text array field defined like this t.text "widgets", default: [], array: true and a permitted param in my controller params.require(:model).permit(widgets: []) In my view I can build values to put into this array using…
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
4
votes
2 answers

What should be in database.yml to use rails without database?

I am using rails3 without database and wondering what should be in database.yml to pass rake test. It fails now. I didn't specify anything in the file yet. My models are going to use ActiveModel. class User #…
Istvan
  • 7,500
  • 9
  • 59
  • 109
4
votes
2 answers

How to access instance variable from a Rails model?

I've defined an instance variable in my ApplicationController like that: @team = Team.find(params[:team_id]) Now, in my EventsCreator model I'd like to access @team from above: class EventsCreator # ... def team_name name = @team.name …
mabu
  • 286
  • 8
  • 26
4
votes
3 answers

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…
jkreeftmeijer
  • 105
  • 2
  • 9