The model Claim consists of a large number of fields.
In claim controller index action querying claims with the only function, fields that are printed in the index list are querying from the db
#claims = #claims.only(:nice_id, :brand_id, etc.. :status).desc(:_id)
The class 'Document' embedded in claims, many document embedded in claims.
I need to include documents in the claim to resultant set. what changes are needed to make in my query?
Used Mongoid in place of Active Model,
mongoid version 3, rails version 3.2
Well, for me, on mongoid 3, adding the association name or its alias (stored_as) (if any) to only works:
#claims = #claims.only(:nice_id, :brand_id, ..., :documents).desc(:_id)
Related
I have to access a legacy database from within a Rails 5.2 project. Unfortunately I can not change any table column names and the table contains a column with the name hash which doesn't work with ActiveRecord (is will throw an error because of hash which is an existing method). I don't need that column but I can neither rename nor delete it either.
Is there a way to tell ActiveRecord to not use the hash field of a given table?
You can use the ignored_columns method that was added to Ruby on Rails in version 5.0 to ignore columns from the database. Quote from the docs:
ignored_columns=(columns)
Sets the columns names the model should ignore. Ignored columns won't have attribute accessors defined, and won't be referenced in SQL queries.
Just add the following to your model:
class MyModel < ApplicationRecord
self.ignored_columns = %w(hash)
end
I added a new column for a rails model and recreated the index but it was not included in searching. so i checked in with browser gui that shows the column listing in left panel but not in the main index search. also interestingly paperclip field is not recognized it goes with the same name as has_attached_file.so how to manually add columns for indexing in a active record model. also used mapping but no use.
The problem was with to_indexed_json method.
as tire uses active records to_json method which i had overridden. when i added the new column in rails migration i never added it to the overridden as_json method.
for further details
ElasticSearch & Tire: Using Mapping and to_indexed_json
Is there any gem/plugin for ruby on rails which gives the ability to define custom fields in a model at runtime with no need to change the model itself for every different field.
I'm looking for something like Redmine acts_as_customizable plugin which is packaged as a gem usable in the rails way, i.e.
gem 'gemname'
rails g something
rails db:migrate
class Model < ActiveRecord::Base
acts_as_something
end
Here are the CustomField and the CustomValue classes used in Redmine.
Edit:
Since my question is not clear I add a brief use case which explains my need better:
I want users to be able to design their own forms, and collect data
submitted on those forms. An important decision is the design of how
these custom dynamic records are stored and accessed.
Taken from here, in this article approach the problem with different ideas, but they all have drawbacks. For this reason I'm asking if the issue has been approached in some gem with no need to rethink the whole problem.
I'm not aware of a gem that does this, but serialize works quite well and it's a built-in. You get a NoSQL-ish document store backed by JSON/YAML.
If you allow user to create a custom form, you can pass nested arrays et cetera directly into the attribute. However, if you need to validate the structure, you're on your own.
I'm afraid it could be tricky and complicated to do it in ActiveRecoand (generally in standard relational database). Take a look at http://mongoid.org/docs/documents/dynamic.html - this mechanism is using nosql feature.
You can also may try the following trick:
1/ Serialize a hash with your custom fields in the database column, for example { :foo => 'bar', :fiz => 'biz' }
2/ After load a record from database do some metaprogramming and define corresponding methods on the record's singleton class, for instance (assume that custom fields are stored and serialized in custom_fields column):
after_initialize :define_custom_methods
# ..or other the most convinient callback
def define_custom_methods
# this trick will open record's singleton class
singleton_class = (class << self; self; end)
# iterate through custom values and define dynamic methods
custom_fields.each_with_key do |key, value|
singleton_class.send(:define_method, key) do
value
end
end
end
Since rails 3.2 you can use store method. Just include following in your model:
store :properties, accessors: [:property1, :property2, :property3...]
You only need to change your model once (to add properties field to db table). You can add more properties later without altering the schema.
The way this works is by serializing properties hash into YAML and saving it into database. It it suitable for most cases, but not if you'd like to use these values in db queries later.
I don't know a gem, but this can be accomplished be creating a table called custom_fields with a name column and possibly a datatype column if you wanted to restrict fields by datatype.
Then you create a join table for a custom field to your desired table and a value and do whatever validations you want.
I am using Ruby on Rails 3.0.7 and I followed this post about handling an "has_many :through => checkboxes" in which, in order to create user-group relationship records for membership purposes, is just passed a group_ids parameter (that is an array of id values) from check box input fields to the #user.save method. Using that code all works good in a "RoR magical\automatic way" (RoR set properly user_id values in the related memberships database table).
However, on saving, I would like to add some extra information to Membership records. That is, (following that post content) when it creates the associated records in the memberships database table (which database table has id, user_id and group_id columns) I would like to save an extra description information (which, for example, can be represented by a description column in that memberships table).
Is it possible to accomplish that by keeping use of the "RoR magical\automatic way"?
I would use the ActiveRecord Callbacks to achieve this :
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
I'm trying to turn a database record into an exportable document, such that I can reimport it and update the database. I'd just use CSV but it's also nested, say Country has_many Provinces has_many Cities.
I've tried dumping YAML per this earlier question:
File.open("#{RAILS_ROOT}/lib/tasks/canada.yml", 'w') do |file|
country = Country.find(1)
country.provinces
country.cities
YAML::dump(country, file)
end
But when I load, it doesn't:
country = YAML.load_file("#{RAILS_ROOT}/lib/tasks/canada.yml")
I even tried hacking the new_record flag per this article but it doesn't change anything. I'm using Rails 3.x.
This must be something other people have done before. How? Or do I need to approach the problem differently?
I guess the problem is that country.save will not produce a new record in the database? You can try Country.create!(country.serializable_hash). That will at least create a new record based on the attributes of country. The country id will be changed and I guess nested objects (Province, City) will not be handled properly.