tire rails active record model omitting some columns - ruby-on-rails

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

Related

Deal with a column name 'hash' in a legacy database

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

how include embedded Document in rails only query

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)

Elasticsearch not indexing field named “type” in DB

With single table inheritance, facing an issue with indexing a specific column named "type" in a table. The table has two cols of interest (among others), "product_name" and "type". The "product_name" field is indexed properly , the "type" field is not getting indexed at all - any ideas on troubleshooting this? Using elasticsearch-ruby gem with Ruby on Rails.
Assuming you are using elasticsearch-model to index your rails models the type column is excluded by default. Basically, as_json gets called on your model to provide fields for elasticsearch to add to the index.
In order to add fields to the index that aren't returned in as_json you'll need to provide an implementation of as_indexed_json. The process is described under Model serialization in the readme.
You'll probably need to do something like:
def as_indexed_json(options = {})
as_json(methods: :type)
end
This will add the type to the json that is used to index the object.

Ruby on Rails - how to instantiate an empty model and populate the object

I am trying to create an empty model, populate it and then add it to the database.
I am looking through Google for the syntax for instantiating a simple model that I can set fields in, but there does not seem to be much documentation for that.
Is that just not the intended useage pattern? If it is, how can I just create the empty model?
Thanks!
An ActiveRecord model works based on what fields it's related table has in your db. If you have no db yet you have no fields. The usage pattern goes like this:
$ rails g model client name:string
#stuff happens
$ rake db:migrate
You now have a model associated with a clients table that has a string attribute called name.
Now in your controller you can use this by
#client = Client.new
#client.name = "foo"
#client.save
Which will create the model object, set the name, and persist it to the db
You should read up on the Rails Guides. Your current issue is covered at this link, but you really need to read up on getting started.

Rails gem/plugin for dynamic custom fields in model

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.

Resources