globalize with search_cop - unknown attribute - ruby-on-rails

I'm trying to use the globalize gem and search_cop together. In my model I have:
class Museum < ApplicationRecord
include SearchCop
has_one_attached :hero_image
translates :name, :address, :description, :facilities, :hours, :tickets
search_scope :search do
attributes :name, :address
options :name, :type => :fulltext
options :address, :type => :fulltext
end
end
But when I go to search I get:
irb(main):006:0> Museum.search("art")
SearchCop::UnknownAttribute: Unknown attribute museums.name
Is it possible to use Globalize and SearchCop together? if so, how do I specify the translated fields to search on?

To use Globalize with SearchCop you need to define the translated attributes through their association. So something like:
search_scope :search do
attributes name: "translations.name", address: "translations.address"
options :name, :type => :fulltext
options :address, :type => :fulltext
end

Related

Limit globalize_accessor locales to arguments from controller/view

I'm using the globalize_accessor gem to render a form that has multilingual inputs for a :name field, and would like to limit the languages a user can input based on which Languages have been passed as arguments to the globalize_attribute_names method. Is something like this possible?
Here's what I have so far:
chart.rb
class Chart < ActiveRecord::Base
belongs_to :chart_type
belongs_to :section
has_many :data_points, dependent: :destroy
translates :name
globalize_accessors :attributes => [:name]
validates :name, presence: true
accepts_nested_attributes_for :data_points
end
charts/new.html.haml
= simple_form_for chart, html: {multipart: true} do |f|
- Chart.globalize_attribute_names.each do |lang|
= f.input lang
= f.input :chart_type, collection: #chart_types
= f.input :section, collection: #sections, label_method: :heading
= f.simple_fields_for :data_points, chart.data_points.build do |c|
= c.input :file, as: :file
= f.submit
UPDATE
To my knowledge, the only way to define locales for translation is to pass them in explicitly at the model level. When trying to use a method to define these however, I see an undefined local variable or method error. The method I'm using currently is for experimentation, and an ideal use case would have this method call the Chart's parent and return the acceptable languages for it. Here's the updated code:
class Chart < ActiveRecord::Base
belongs_to :chart_type
belongs_to :section
has_many :data_points, dependent: :destroy
translates :name
globalize_accessors :attributes => [:name], locales: languages
validates :name, presence: true
accepts_nested_attributes_for :data_points
def self.languages
[:en, :fr]
end
You're seeing an undefined local variable or method error because you are defining the method languages after you use it in the class. Just define the method above the globalize_accessors call and you should be fine:
class Chart < ActiveRecord::Base
...
def self.languages # <= define this first
[:en, :fr]
end
globalize_accessors :attributes => [:name], locales: languages

How to include only some attributes in Mongoid referenced n-n within to_json

class User
include Mongoid::Document
has_and_belongs_to_many :contacts, class_name: 'User'
field :username, :type => String
field :email, :type => String
field :time_zone, :type => String
What I want is when I call current_user.contacts.to_json to get only the username and email attributes for each contact. I tried to override to_json but doesn't seems to change anything. Any ideas?
if i understand, you can write your query like this.
User.find(:all, :select => 'email, username')

persist embeds_one relation mongoid

class Order
include Mongoid::Document
include Mongoid::Timestamps
#relationships
embeds_one :user_detail
#fields
field :description
#validations
validates :user_detail, presence: true
end
This the embedded object in order:
class UserDetail
include Mongoid::Document
include Mongoid::Timestamps
#fields
field :name, :type => String
field :zip_code, :type => String
field :email, :type => String
# Relationships
embedded_in :order
#validations
validates_presence_of :name, :zip_code, :email
end
I want save/persist on mongodb order object with user_detail object embedded_in order object.
I have tried with:
order = Order.new(description: "checking description")
order.user_detail = Order.new(:name => "John", :zip_code => "26545", :email => "john#john.com")
order.save!
but I get validation fail:
o.save!
Mongoid::Errors::Validations:
Problem:
Validation of Order failed.
Summary:
The following errors were found: User detail is invalid
Resolution:
Try persisting the document with valid data or remove the validations....
How can I fix this problem? I'm using mongoid 3.x
Should be:
order = Order.new(description: "checking description")
order.user_detail = UserDetail.new(:name => "John", :zip_code => "26545", :email => "john#john.com")
order.save!
You had Order.new for OrderDetail.new
You do not need to manually create user_detail using
order.user_detail = UserDetail.new...
order.save!
The embedded user_detail will be created automatically if u add autobuild attribute
embeds_one :user_detail autobuild: true
If u wanna persist user_detail in database as well, do not forget to add
validates_presence_of :user_detail
or you will not see the persisted user_detail in mongo db.

Tire gem: index a Rails parent/child relationship in Elasticsearch

Having a tough time wrapping my head around Tire's syntax, that of Elasticsearch and how they map together.
I have successfully indexed PDFs in a Rails app via Tire. But I need to break the full reports down into individual pages so queries can be more granular. It's easy enough to split the PDFs into individual pages and add them to a Page model that belongs_to the full Report model. What I'm struggling with is how to set up the mapping and where?!? I'd like to take advantage of Elasticsearch's Parent Field mapping so I can realize this ultimate goal.
Hoping someone can set me straight.
Report model (this is working for me if I index an entire PDF as the :attachment):
class Report < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
has_many :pages, :dependent => :destroy
attr_accessible :filename, :title
tire.mapping do
indexes :id, :type =>'integer'
indexes :title
indexes :attachment, :type => 'attachment',
:fields => {
:content_type => { :store => 'yes' },
:author => { :store => 'yes' },
:title => { :store => 'yes' },
:attachment => { :term_vector => 'with_positions_offsets', :store => 'yes' },
:date => { :store => 'yes' }
}
end
...
end
Page model:
class Page < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
belongs_to :report
attr_accessible :filename, :page_num,
tire.mapping do
indexes :id, :type => 'integer'
indexes :page_num, :type => 'integer'
indexes :report_id, :type => 'integer' ###<== how is this associated with _parent?
indexes :attachment, :type => 'attachment',
:fields => {
...
...
end
end

ActiveModel::MassAssignmentSecurity::Error in CustomersController#create (attr_accessible is set)

In my controller, I've got error when create action and try create model [can't mass-assignment], but
in my spec, my test of mass-assignment model its pass!?!
My Model:
class Customer < ActiveRecord::Base
attr_accessible :doc, :doc_rg, :name, :birthday, :name_sec, :address, :state_id, :city_id, :district_id,
:customer_pj, :is_customer, :segment_id, :activity_id, :person_type, :person_id
belongs_to :person , :polymorphic => true, dependent: :destroy
has_many :histories
has_many :emails
def self.search(search)
if search
conditions = []
conditions << ['name LIKE ?', "%#{search}%"]
find(:all, :conditions => conditions)
else
find(:all)
end
end
end
I`ve tired set attr_accessible in controller too, in my randomized way.
the Controller:
class CustomersController < ApplicationController
include ActiveModel::MassAssignmentSecurity
attr_accessible :doc, :doc_rg, :name, :birthday, :name_sec, :address, :state_id, :city_id, :district_id, :customer_pj, :is_customer
autocomplete :business_segment, :name, :full => true
autocomplete :business_activity, :name, :full => true
[...]
end
The test, my passed test
describe "accessible attributes" do
it "should allow access to basics fields" do
expect do
#customer.save
end.should_not raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
The error:
ActiveModel::MassAssignmentSecurity::Error in CustomersController#create
Can't mass-assign protected attributes: doc, doc_rg, name_sec, address, state_id, city_id, district_id, customer_pj, is_customer
https://github.com/megabga/crm
1.9.2p320
Rails 3.2
MacOS
pg
my bad, in my controller its setting an oldest class. Then old class don`t have attributes passing in parameters. Sorry!

Resources