activemodel

How to access params in the callback of a Rails model?

匆匆过客 提交于 2019-12-03 12:41:07
I have a callback of a model that needs to create a dependent object based on another field entered in the form. But params is undefined in the callback method. Is there another way to access it? What's the proper way to pass a callback method parameters from a form? class User < ActiveRecord::Base attr_accessible :name has_many :enrollments after_create :create_enrollment_log private def create_enrollment_log enrollments.create!(status: 'signed up', started: params[:sign_up_date]) end end params are not accessible in models, even if you pass them as a parameter then it would be consider as

ActiveModel - View - Controller in Rails instead of ActiveRecord?

三世轮回 提交于 2019-12-03 11:36:20
问题 I'm trying to use ActiveModel instead of ActiveRecord for my models because I do not want my models to have anything to do with the database. Below is my model: class User include ActiveModel::Validations validates :name, :presence => true validates :email, :presence => true validates :password, :presence => true, :confirmation => true attr_accessor :name, :email, :password, :salt def initialize(attributes = {}) @name = attributes[:name] @email = attributes[:email] @password = attributes[

Rails - Polymorphic Favorites (user can favorite different models)

ぐ巨炮叔叔 提交于 2019-12-03 10:14:44
问题 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 belongs_to :user belongs_to :category belongs_to :business belongs_to :ad_channel belongs_to :location belongs_to :offer end The user model: class User < ActiveRecord::Base has_many :favorites, as: :favoritable end And one example model of something

Use ActiveModel::Serializers to include two parent json arrays

自古美人都是妖i 提交于 2019-12-03 09:10:52
问题 I'm trying to send my front-end application json that looks like this: { facilities: [ {id: 5, name: 'happy days ranch', location: { address: '1424 Pastoral Lane', zipcode: '25245'}, instructor_ids: [2, 4, 9]} ], instructors: [ {id: 4, name: 'Johnny Pheonix', skill: '8', picture: 'aws_url', facility_ids: [5, 8, 12} ] } Things I have tried render :json => @facilities The serializer discovers this. Yay! But this does not include any instructors render :json => {facilities: @facilities,

Efficient way to report record validation warnings as well as errors?

喜欢而已 提交于 2019-12-03 07:01:12
问题 I've got a Rails project where, as in most apps, we have a number of hard-and-fast validation rules to which all objects must conform before being persisted. Naturally, ActiveModel's Validations are perfect for that – we're using a combination of Rails defaults and our own hand-rolled validations. More and more, though, we're coming up against use cases where we would like to alert the user to cases where, while their data is not invalid in the strictest sense, there are elements which they

Track dirty for not-persisted attribute in an ActiveRecord object in rails

自古美人都是妖i 提交于 2019-12-03 05:27:25
I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like: class Foo < ActiveRecord::Base attr_accessor :bar end I would like to be able to track changes to 'bar', with methods like 'bar_changed?', as provided by ActiveModel Dirty. The problem is that when I try to implement Dirty on this object, as described in the docs , I'm getting an error as both ActiveRecord and ActiveModel have defined define_attribute_methods , but with different number of parameters, so I'm getting an error when trying to invoke define_attribute_methods [:bar] . I

How to setup a one to many relationship?

江枫思渺然 提交于 2019-12-03 02:28:40
I have the following models: User (id, name, network_id) Network(id, title) What kind of Rails model assoc do I need to add so that I can do: @user.network.title @network.users Thanks daniel so network has_many users and a user belongs_to network. Just add a network_id to users table if you still haven't and also since it's a foreign_key is worth indexing it. rails generate migration AddNetworkIdToUsers class AddNetworkIdToUsers < ActiveRecord::Migration def change add_column :users, :network_id, :integer add_index :users, :network_id end end In the network model do: class Network <

Rails - Polymorphic Favorites (user can favorite different models)

让人想犯罪 __ 提交于 2019-12-03 00:45:22
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 belongs_to :user belongs_to :category belongs_to :business belongs_to :ad_channel belongs_to :location belongs_to :offer end The user model: class User < ActiveRecord::Base has_many :favorites, as: :favoritable end And one example model of something that can be favorited: class Category < ActiveRecord::Base has_many :sub_categories has_many :ad

Efficient way to report record validation warnings as well as errors?

吃可爱长大的小学妹 提交于 2019-12-02 20:39:14
I've got a Rails project where, as in most apps, we have a number of hard-and-fast validation rules to which all objects must conform before being persisted. Naturally, ActiveModel's Validations are perfect for that – we're using a combination of Rails defaults and our own hand-rolled validations. More and more, though, we're coming up against use cases where we would like to alert the user to cases where, while their data is not invalid in the strictest sense, there are elements which they should review, but which shouldn't in themselves prevent record persistence from occurring. A couple of

Rails accepts_nested_attributes count validation

寵の児 提交于 2019-12-01 20:49:23
问题 I've got three models. Sales, items, and images. I'd like to validate that when a sale is created there are at least three photos per sale and one or more items. What would be the best way to achieve this? Sales Model: class Sale < ActiveRecord::Base has_many :items, :dependent => :destroy has_many :images, :through => :items accepts_nested_attributes_for :items, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true end Items Model: class Item < ActiveRecord::Base belongs_to