Rails Admin - List, Scope on association - ruby-on-rails

I have categories and articles. They have a HABTM relation. I would like the possibility of listing each category and the articles that have relation to that article in the list view of articles.
How can I accomplish this?
I have tried with scopes, however this require a function?
https://github.com/mathieul/rails_admin

I would create a custom method and then include in fields
class Article
include Mongoid::Document
field :title, type: String
field :body, type: String
has_and_belongs_to_many :categories
def category_articles
output = []
categories.each do |cat|
output << cat.articles.pluck(:title)
end
return output.flatten.join(', ')
end
rails_admin do
list do
include_all_fields
field :category_articles
end
end
end

Related

Postgresql JSONB nested form ruby on rails

I have product as active record table and option_type as activemodel model. option types is an array of objects as follows,
[
{name: 'color', values: ['red', 'blue']},
{name: 'size', values: ['small', 'medium']}
]
class OptionType
include ActiveModel::Model
attr_accessor :name, :values, :default_value
def initialize(**attrs)
attrs.each do |attr, value|
send("#{attr}=", value)
end
end
def attributes
[:name, :values, :default_value].inject({}) do |hash, attr|
hash[attr] = send(attr)
hash
end
end
class ArraySerializer
class << self
def load(arr)
arr.map do |item|
OptionType.new(item)
end
end
def dump(arr)
arr.map(&:attributes)
end
end
end
end
I want to desing a form_for with nested form for option_types so that user can add various option names and it's values. How to do it?
reference links are as follow,
Validation of objects inside array of jsonb objects with RubyOnRails
I know this isn't the answer you're hoping for but instead of just tossing the whole lot into a JSONB column and hoping for the best you should model it as far as possible in a relational way:
class Product < ApplicationRecord
has_many :options
has_many :product_options, through: :options
end
# rails g model option name:string product:belongs_to
class Option < ApplicationRecord
belongs_to :product
has_many :product_options
end
# rails g model product_option option:belongs_to name:string ean:string
class ProductOption < ApplicationRecord
belongs_to :option
has_one :product, through: :options
end
If your data is actually structured enough that you can write code that references its attributes then a JSON column isn't the right answer. JSON/arrays aren't the right answer for setting up assocations either.
This lets you use foreign keys to maintain referential integrity and has a somewhat sane schema and queries instead of just dealing with a totally unstructed mess. If you then have to deal with an attribute that can have varying types like for example an option that can be string, boolean or numerical you can use a JSON column to store the values to somewhat mitigate the disadvantages of the old EAV pattern.
Creating variants of a product could then either be done via a seperate form, nested attributes or AJAX depending on your requirements.

Rails 4 searchkick with many-to-many relationsip

How to implement a Category filter for searchkick. Basically I have an input field that takes the query term, but I also want a dropdown that users can pick a category to search from or search in ALL categories. There is a many-to-many relationship between posts and categories
My models are:
-- post.rb
class Post < ActiveRecord::Base
has_many :post_categories
has_many :categories, through: :post_categories
searchkick text_start: [:title]
end
-- category.rb
class Category < ActiveRecord::Base
has_many :post_categories
has_many :posts, through: :post_categories
end
--post_category.rb
class PostCategory < ActiveRecord::Base
belongs_to :post
belongs_to :category
end
Now in my posts_controller index action, I have the following, which works so far by returning all posts that match the query parameter, or returns all posts if no query parameter is provided in the search input.
class PostsController < ApplicationController
def index
query = params[:q].presence || "*"
#posts = Post.search (query)
end
end
This works well so far. But I also want to add a category filter in the view, so that the user can choose to search for the query string within a particular category, or search in all categories if no category is selected. Thanks in advance.
As per searchkick documentation, you can add parameters to .search query - see here section Queries, specifically where. Sample from docs:
Product.search "apples", where: {in_stock: true}, limit: 10, offset: 50
Should be smt like
Post.search query, where: {category: [some_array]}
Note - searchkick gem converts conditions from where statement to ElasticSearch filters (see here)
Update - for searching by attributes of related objects (not model itself), you should include its fields into search_index - see sample here
Add the category titles to the search_data method.
class Project < ActiveRecord::Base
def search_data
attributes.merge(
categories_title: categories.map(&:title)
)
end
end
Also this question on related topic
By default search_data of searchkick is return of serializable_hash model call -see sources for reference.
def search_data
respond_to?(:to_hash) ? to_hash : serializable_hash
end unless method_defined?(:search_data)
Which does not include anything of associations by default, unless passed with :include parameter - source
def serializable_hash(options = nil)
...
serializable_add_includes(options) do ..
end
def serializable_add_includes(options = {}) #:nodoc:
return unless includes = options[:include]
...
end

How to migrate from belongs_to, to embedded_in in Mongoid?

If one first build their models with a belong_to and has_many association and then realized they need to move to a embedded_in and embeds_many association, how would one do this without invalidating thousands of records? Need to migrate them somehow.
I am not so sure my solution is right or not. This is something you might try to accomplish it.
Suppose You have models - like this
#User Model
class User
include Mongoid::Document
has_many :books
end
#Book Model
class Book
include Mongoid::Document
field :title
belongs_to :user
end
At first step I will create another model that is similar to the Book model above but it's embedded instead of referenced.
#EmbedBook Model
class EmbedBook
include Mongoid::Document
field :title
embedded_in :user
end
#User Model (Update with EmbedBook Model)
class User
include Mongoid::Document
embeds_many :embed_books
has_many :books
end
Then create a Mongoid Migration with something like this for the above example
class ReferenceToEmbed < Mongoid::Migration
def up
User.all.each do |user|
user.books.each do |book|
embed_book = user.embed_books.new
embed_book.title = book.title
embed_book.save
book.destroy
end
end
end
def down
# I am not so sure How to reverse this migration so I am skipping it here
end
end
After running the migration. From here you can see that reference books are embedded, but the name for the embedded model is EmbedBook and model Book is still there
So the next step would be to make model book as embed instead.
class Book
include Mongoid::Document
embedded_in :user
field :title
end
class User
include Mongoid::Document
embeds_many :books
embeds_many :embed_books
end
So the next would be to migrate embedbook type to book type
class EmbedBookToBook < Mongoid::Migration
def up
User.all.each do |user|
user.embed_books.each do |embed_book|
book = user.books.new
book.title = embed_book.title
book.save
embed_book.destroy
end
end
def down
# I am skipping this portion. Since I am not so sure how to migrate back.
end
end
Now If you see Book is changed from referenced to embedded.
You can remove EmbedBook model to make the changing complete.
This is just the suggestion. Try this on your development before trying on production. Since, I think there might be something wrong in my suggestion.
10gen has a couple of articles on data modeling which could be useful:
Data Modeling Considerations for MongoDB Applications
Embedded One-to-Many Relationships
Referenced One-to-Many Relationships
MongoDB Data Modeling and Rails
Remember that there are two limitations in MongoDB when it comes to embedding:
the document size-limit is 16MB - this implies a max number of embedded documents, even if you just embed their object-id
if you ever want to search across all embedded documents from the top-level, then don't embed, but use referenced documents instead!
Try these steps:
In User model leave the has_many :books relation, and add the
embedded relation with a different name to not override the books
method.
class User
include Mongoid::Document
has_many :books
embeds_many :embedded_books, :class_name => "Book"
end
Now if you call the embedded_books method from a User instance
mongoid should return an empty array.
Without adding any embedded relation to Book model, write your own
migration script:
class Book
include Mongoid::Document
field :title, type: String
field :price, type: Integer
belongs_to :user
def self.migrate
attributes_to_migrate = ["title","price"] # Use strings not symbols,
# we keep only what we need.
# We skip :user_id field because
# is a field related to belongs_to association.
Book.all.each do |book|
attrs = book.attributes.slice(*attributes_to_migrate)
user = book.user // through belong_to association
user.embedded_book.create!(attrs)
end
end
end
Calling Book.migrate you should have all the Books copied inside each user who was
associated with belongs_to relation.
Now you can remove the has_many and belongs_to relations, and
finally switch to clean embedded solution.
class User
include Mongoid::Document
embeds_many :books
end
class Book
include Mongoid::Document
field :title, type: String
field :price, type: Integer
embedded_in :user
end
I have not tested this solution, but theoretically should work, let me know.
I have a much shorter concise answer:
Let's assume that you have the same models:
#User Model
class User
include Mongoid::Document
has_many :books
end
#Book Model
class Book
include Mongoid::Document
field :title
belongs_to :user
end
So change it to embeds:
#User Model
class User
include Mongoid::Document
embeds_many :books
end
#Book Model
class Book
include Mongoid::Document
field :title
embedded_in :user
end
And generate a mongoid migration like this:
class EmbedBooks < Mongoid::Migration
##attributes_to_migrate = [:title]
def self.up
Book.unscoped.where(:user_id.ne => nil).all.each do |book|
user = User.find book[:user_id]
if user
attrs = book.attributes.slice(*##attributes_to_migrate)
user.books.create! attrs
end
end
end
def self.down
User.unscoped.all.each do |user|
user.books.each do |book|
attrs = ##attributes_to_migrate.reduce({}) do |sym,attr|
sym[attr] = book[attr]
sym
end
attrs[:user] = user
Book.find_or_create_by(**attrs)
end
end
end
end
This works because when you query from class level, it is looking for the top level collection (which still exists even if you change your relations), and the book[:user_id] is a trick to access the document attribute instead of autogenerated methods which also exists as you have not done anything to delete them.
So there you have it, a simple migration from relational to embedded

sort by association field in sunspot

In my app I am using Sunspot for a fulltext search. The problem is that I want to have sorting by association model field. In my case:
class Movie < ActiveRecord::Base
attr_accessible :description, :genre, :name
has_many :premieres
end
and my Premier model has:
belongs_to :movie
Searching by movie name is done by defining the method:
def movie_name
movie.name
end
but when I try to do:
order_by :movie_name, :asc
It says:
No field configured for Premiere with name 'movie_name'
How do I make this sorting available?
you can do it like this
searchable auto_index: true do
text :movie_name do
if self.movie.present?
self.movie.name
end
end
end
then you can use
order_by :movie_name, :asc

Replace ID with text in rails_admin associations

when using rails_admin for associated objects (like has_and_belongs_to) it shows the ID of the object as the association.
This isn't a great deal for the users so I'ld like to change this for showing the text of the associated object.
Is this solvable?
Here a little example:
First Model:
class Menu
include Mongoid::Document
field :date, type: Date
has_and_belongs_to_many :meal
end
Second Model:
class Meal
include Mongoid::Document
field :text, type: String
has_and_belongs_to_many :menu
end
So it shows something like this:
But I'ld love to see the Text of the meals instead.
Simply define a title-method do the trick:
def title
self.text
end
You could use the RailsAdmin DSL object_label_method to change how the field is presented to the user.
In your case, something like this might do the trick:
RailsAdmin.config do |config|
config.model Menu do
list do
field :meal do
pretty_value do
value.text
end
end
end
end
end

Resources