In Rails 4:
Using find() directly on my model generates a query which looks up the slug in the page_translations table:
Page.find('my-title')
SELECT FROM "pages" LEFT OUTER JOIN "page_translations" ...
-> #<Page id: 1 ...>
When I use find through a association, the translation table isn't being used. friendly_id uses the orginal table instead.
#site.pages.find('my-title')
SELECT "pages".* FROM "pages" WHERE "pages"."site_id" = $1 AND "pages"."slug" = 'my-title' LIMIT 1 [["site_id", 1]]
-> ActiveRecord::RecordNotFound
In Rails 3.2 (friendly_id 4.0.10, globalize 3.0.0) it works like this:
#site.pages.find('my-title')
SELECT "pages".* FROM "pages" WHERE "pages"."shop_id" = 1 AND "pages"."slug" = 'my-title' LIMIT 1
SELECT DISTINCT "pages".id, pages.position AS alias_0 FROM "pages" LEFT OUTER JOIN "page_translations" ...
SELECT "pages"."id" AS t0_r0, "pages"."title" AS t0_r1 ... FROM "pages" LEFT OUTER JOIN "page_translations"
-> #<Page id: 1 ...>
See also https://github.com/norman/friendly_id-globalize/issues/1.
Repo owner #parndt is currently busy. So any hints to get this gem working will be greatly appreciated.
Turned out to be a bug in the Globalize gem, which has been fixed shortly.
Related
Hi I m stuck in a situation and I m unable to find a solution, there is a dropbox that displays the tags matched with the string entered but the limit is set for 30 as it displays 30 results only. I want it to increase it but I m unable to find where the limit is applied.
the logs do return the query but I m unable to find it
Started GET "/admin/tag?associated_collection=tags&compact=true¤t_action=update&source_abstract_model=video&source_object_id=7732&query=simula" for 127.0.0.1 at 2021-06-10 22:19:57 -0700
Processing by RailsAdmin::MainController#index as JSON
Parameters: {"associated_collection"=>"tags", "compact"=>"true", "current_action"=>"update", "source_abstract_model"=>"video", "source_object_id"=>"7732", "query"=>"simula", "model_name"=>"tag"}
Admin Load (0.7ms) SELECT `admins`.* FROM `admins` WHERE `admins`.`id` = 14 ORDER BY `admins`.`id` ASC LIMIT 1
Video Load (0.4ms) SELECT `videos`.* FROM `videos` WHERE `videos`.`id` = 7732 ORDER BY `videos`.`id` ASC LIMIT 1
(0.3ms) SELECT COUNT(*) FROM `tags`
Tag Load (1.2ms) SELECT `tags`.* FROM `tags` WHERE ((LOWER(tags.name) LIKE '%simula%') OR (LOWER(tags.ui_name) LIKE '%simula%')) ORDER BY tags.id desc LIMIT 30
can anyone please help me out, how may I increase the limit
You need to configure the field like this
rails_admin do
edit do
field :tags do
associated_collection_scope do
proc do |scope|
scope.limit(600)
end
end
end
end
end
But more importantly i discovered this searching on the rails_admin code, i tried searching for the string ' 30' on the rails admin gem folder and there were not that many results one had the limit.
cd $(bundle show rails_admin)
ag ' 30' | grep limit
This returned
lib/rails_admin/config/fields/association.rb:43: associated_collection_scope_limit = (associated_collection_cache_all ? nil : 30)
So i opened the file and found that the block of code that has that scoping is part of the configuration of all association fields on rails admin so i knew i could just add it to that particular field.
I'm creating a Rails 6.0.2.2 App.
My problem is that if statement in seeds.rb file not works.
Just for curiosity, I added the if statement below.
if Plan.count == 0 # always true -- though it already have a lot of plan data.
Plan.create!(name: 'スタート')
Plan.create!(name: 'ライト')
Plan.create!(name: 'スタンダード')
end
However, every time I execute rake db:seed, it always seeds those data.
When I run rails console, it returns something like...
irb(main):073:0> Plan.count
(0.8ms) SELECT COUNT(*) FROM "plans"
=> 3
irb(main):074:0> Plan.count
(1.0ms) SELECT COUNT(*) FROM "plans"
=> 6
irb(main):076:0> Plan.count
(1.3ms) SELECT COUNT(*) FROM "plans"
=> 9
irb(main):077:0> Plan.count
(1.5ms) SELECT COUNT(*) FROM "plans"
=> 12
Why Plan.all.count returns 0 wrongly?
Very strange. Create an array of plant names, iterate and create like below
["Plant 1", "Plant 2", "Plant 3"].each do |plant|
Plant.find_or_create_by!(name: plant)
end
I have little understanding problem:
I have Channel and Lecturer, where a Channel :has_and_belongs_to_many :lecturers.
I want to get all Channels where Lecturer.id is lect.id.
2.3.0 :235 > Channel.where(:lecturers => { :id => 2 })
Channel Load (0.1ms) SELECT "channels".* FROM "channels" WHERE "lecturers"."id" = 2
SQLite3::SQLException: no such column: lecturers.id: SELECT "channels".* FROM "channels" WHERE "lecturers"."id" = 2
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: lecturers.id: SELECT "channels".* FROM "channels" WHERE "lecturers"."id" = 2
This does not work and I feel like I don't understand the core concept, since I can do
2.3.0 :231 > Channel.first.lecturer
=> #<Lecturer id: 2, name: "Albert Einstein">
What am I missing?
You'll want to join the tables to do this query. See the docs
Channel.joins(:lecturer).where(lecturers: {id: lect.id})
I have a Genre model which have it's name translated in a genre_translations table (using the globalize gem)
I'm trying to indexing the model using the elasticsearch-rails gem
def as_indexed_json(options = {})
as_json(
only: %i(type available),
methods: %i(name),
)
end
but when I do Genre.import I get the following on my rails console:
[1] pry(main)> Genre.import
Genre Load (27.1ms) SELECT "genres".* FROM "genres" ORDER BY "genres"."id" ASC LIMIT 1000
Genre::Translation Load (23.9ms) SELECT "genre_translations".* FROM "genre_translations" WHERE "genre_translations"."genre_id" = $1 [["genre_id", 1]]
Genre::Translation Load (0.3ms) SELECT "genre_translations".* FROM "genre_translations" WHERE "genre_translations"."genre_id" = $1 [["genre_id", 2]]
Genre::Translation Load (0.3ms) SELECT "genre_translations".* FROM "genre_translations" WHERE "genre_translations"."genre_id" = $1 [["genre_id", 3]]
...
Any suggestion on how to index all the Genre items with a join to avoid the N+1 behaviour?
From the doc here
# #example Pass an ActiveRecord query to limit the imported records
#
# Article.import query: -> { where(author_id: author_id) }
So you could do:
Genre.import query: -> { includes(:translations) }
In $ rails console I noticed that multiple queries are being run when I save a record:
ruby-1.9.2-p180 :001 > ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #<...>
ruby-1.9.2-p180 :002 > p = Project.first
Project Load (0.3ms) SELECT `projects`.* FROM `projects` LIMIT 1
=> #<Project id: 1, category_id: 1, qualified_at: "2011-12-14 15:06:29", ...>
ruby-1.9.2-p180 :003 > p.qualified_at = Time.now
=> 2011-12-14 10:11:42 -0500
ruby-1.9.2-p180 :004 > p.save
SQL (0.2ms) BEGIN
SQL (1.5ms) SHOW TABLES
AREL (0.3ms) UPDATE `projects` SET `qualified_at` = '2011-12-14 15:11:42', `updated_at` = '2011-12-14 15:11:47' WHERE `projects`.`id` = 1
Category Load (0.3ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 ORDER BY name LIMIT 1
ProjectPerson Load (0.4ms) SELECT `project_people`.* FROM `project_people` WHERE (`project_people`.project_id = 1 AND (project_people.is_client = 1)) LIMIT 1
Person Load (0.3ms) SELECT `people`.* FROM `people` WHERE (`people`.`id` = 2)
ProjectTag Load (0.4ms) SELECT DISTINCT `project_tags`.tag_id FROM `project_tags` WHERE (`project_tags`.project_id = 1)
SQL (0.5ms) COMMIT
=> true
I don't have before or after filters in my Project model, and I'm not using an Observer. Obviously these queries relate to associations of the Project model, but why are the queries being run? Not sure what else to consider. Thanks.
I just figured it out... I'm using the sunspot_rails gem and it was updating the index for that record :) These were associations that were referenced in my index definition.
I'll keep the question in case anyone else happens to come across a similar problem.