activemodel

How to setup a one to many relationship?

巧了我就是萌 提交于 2019-12-20 11:19:19
问题 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 回答1: 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

What is the ActiveModel method attribute “_was” used for?

自闭症网瘾萝莉.ら 提交于 2019-12-18 13:07:56
问题 When using autocomplete in the console, I often see " _was " postpended to my attributes. But I can't find any documentation or best practices for usage. What does it do and how should it be used? Example: user.fname has the method user.fname_was Using source_location, I've tracked it down to: active_model/attribute_methods.rb", line 296 but there isn't anything specific. 回答1: That is a part of ActiveModel::Dirty You can see it here https://github.com/rails/rails/blob

form_for non-AR model - fields_for Array attribute doesn't iterate

不问归期 提交于 2019-12-18 12:29:19
问题 I'm having trouble getting fields_for to work on an Array attribute of a non-ActiveRecord model. Distilled down, I have to following: models/parent.rb class Parent extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations extend ActiveModel::Translation attr_accessor :bars end controllers/parent_controller.rb def new_parent @parent = Parent.new @parent.bars = ["hello", "world"] render 'new_parent' end views/new_parent.html.haml = form_for @parent, :url => new

ActiveModel::MissingAttributeError occurs after deploying and then goes away after a while

萝らか妹 提交于 2019-12-18 11:16:20
问题 I have a Rails 3.0.9 app that, once it is deployed, suffers from a bunch of ActiveModel::MissingAttributeErrors that crop up causing 500s. The errors occur fairly randomly, sometimes a page will load, other times it won't, but the attributes are all existing attributes in the database and should be found. The strange part is that after a while, the errors go away. Suddenly, they stop causing an issue. I have searched about for a solution to this, but this error mostly occurs either when

How to implement multiple different serializers for same model using ActiveModel::Serializers?

a 夏天 提交于 2019-12-17 17:26:53
问题 Let's say you're implementing a REST API in Rails. When serving a collection, you might want to only include a few attributes: /people But when serving a single resource, you want to include all the attributes: /people/1 I don't see how to do that using ActiveModel::Serializers, since the examples all use the pattern of defining one serializer per model (with a standard naming convention) and having AMS automatically use the right one in the controller when you do: render json: @people or:

Versioning ActiveModel::Serializer

大憨熊 提交于 2019-12-13 19:53:40
问题 I'm using the gem active_model_serializers and I'm facing some issues with versioning. Controllers In app/controllers/v1/contracts_controller.rb module V1 class ContractsController < ApiController def index @contracts = Contract.all render json: @contracts end end end In app/controllers/v2/contracts_controller.rb module V2 class ContractsController < ApiController def index @contracts = Contract.all render json: @contracts end end end Serializers In app/serializers/v1/contract_serializer.rb

Increase ActiveModel ID Range to 8 byte

喜欢而已 提交于 2019-12-13 06:39:20
问题 I've been digging around to see how I could have all my newly and subsequent Model id's to have a limit of 8 byte. Answers show how to when adding a new table column; I want whenever I create a new Model , it would automatically has a limit of 8 byte. Possible? When creating a new model, I get: ActiveModel::RangeError: 36565651767 is out of range for ActiveModel::Type::Integer with limit 4 Where to change this limit from 4 to 8? 回答1: A possible duplicate but since there will be errors: you

Build a table which has a Field of User_ids, and then querying for a particular User_Id

谁说胖子不能爱 提交于 2019-12-13 01:57:55
问题 I need some help building a table and then getting data from that table in Rails 3. Here's the break down: Models - 3 models involved here they are: Thread has many participants Participants belong to thread Users Activity table: id | thread_id | participants Example records would look something like: 1 | 300 | 3,1,5,67,13 2 | 333 | 3,12 3 | 433 | 1,12 4 | 553 | 1,12, 67 Where participants, is a list of user_ids, if there is a better way to store the user_ids please let me know. I haven't

I18n deprecation warning when using ActiveModel without Rails

寵の児 提交于 2019-12-13 01:24:11
问题 When I run Rspec on my models, I get this warning: [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. I saw a similar question where the solution was to set config.i18n.enforce_available_locales or I18n.config.enforce_available_locales in my config/application.rb file. I tried both, but I still get the warning. The test that gives me the

Mongoid: ActiveModel Numericality Validation, allow_nil does not work

送分小仙女□ 提交于 2019-12-13 00:54:22
问题 I've defined a Mongoid model with an Integer field for which i validate numericality like this # source.rb class Source field :code, type: Integer validates_numericality_of :code, allow_nil: true The purpose of allow_nil is to validate fields which are present & ignore nil values. But here, allow_nil completely bypasses the numericality check object = Source.new object.code = "ABC" object.valid? => true object => #<Source _id: 50d00b2d81ee9eae46000001, _type: nil, code: 0> In activerecord,