norman/friendly_id-globalize

find() through association isn't using translation table

Closed this issue · 15 comments

rvrm commented

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 ...>

This is "normal" because find is not surcharged in Friendly ID 5. A proxy must be used like this 👍

User.friendly.find(params[:id])

I don't know if it's working on an association ?
I did have the same problem, and a clear exemple of the new way to use friendly_id-globalize would be much appreciated !

Per the new Friendly_id 5 config file, setting
config.use :finders
is said to activate the :finders method on all models.

Unfortunately the OP's problem persists. Finding through an association seems currently not possible with v5.
@parndt to the rescue.

@yadoga Finding through an association is partly broken because of bugs in Rails 4.0 that will be fixed in 4.0.1. You might want to check and see if the issue described in norman/friendly_id#452 is what's affecting you.

Thanks for your reply @norman. I'm afraid I'm using Rails 4.0.1 here on my end and the problem persists. Maybe you meant the "5.0.1" version of Friendly_id instead?

I believe he meant 5.0.1 wich will depend on 4.0.1

On 4 nov. 2013, at 18:04, yadoga notifications@github.com wrote:

Thanks for your reply @norman. I'm afraid I'm using Rails 4.0.1 here on my end and the problem persists. Maybe you meant the "5.0.1" version of Friendly_id instead?


Reply to this email directly or view it on GitHub.

Norman speaks of Active Record 4.0.1 in the referenced thread above. Curiously it's now out and does not seem to solve the issue. I'd love my friendly_id healthy again. Please please with sugar on top.

Unfortunately I've never used the globalize functionality and for the next several months need to focus on maintaining Haml. I spent about 2 months working on FriendlyId to the exclusion of Haml and now I need to catch up with pending issues and requests over there. @parndt, the primary maintainer of Globalize and its integration with FriendlyId, is busy with work and other high priority open source projects. It would be fantastic if somebody with a little time to spare could give us a hand with this.

Does Rails 4.0.1 not fix this issue?

Hi Philip, glad to see you around! Unfortunately the error persists with Rails 4.0.1. Just double checked this. Finding a translated title through an association still returns 'RecordNotFound' as described in the first comment.

Can anybody make a simple reproducing test case that can be added to the library? This makes it a lot easier to fix. The original post assumes I have a @site and Page model already set up.

ok, setting one up.

Test Case here:
https://github.com/rvrm/Testapp

In this case as "User has_many Pages".

Outch, too late a night for me. Setting up a real test case now, not only a test app.

Testing currently gives me other unrelated errors (Gem.source_index, …), so I'm not able to provide a valid test for the moment. Can anybody give a hand here and set one up?

rvrm commented

This seems to be general globalize issue. I added it here: globalize/globalize#303