0

I have some tables: users, posts, translations, vocabularies a user can create posts, make translations for words on posts and can mark translations of others as vocabularies for review. table vocabularies only has 2 columns (user_id,translation_id) In controller I declare variable:

@translations = @post.translations.where("translations.words=?", params[:words], params[:end]).joins("LEFT OUTER JOIN vocabularies ON vocabularies.translation_id = translations.id AND vocabularies.user_id = 6").select("translations.*")

And get errors

SQLite3::SQLException: near "*": syntax error: SELECT COUNT(translations.*) FROM "translations" LEFT OUTER JOIN vocabularies ON vocabularies.translation_id = translations.id AND vocabularies.user_id = 6 WHERE "translations"."post_id" = ? AND (translations.words='hello')

One more question, because the :translations and :vocabularies both have the same column user_id, so how can alias user_id of :vocabularies as learner_id? I tried to run this command but it still got error: SQLite3::SQLException: near "as":

@translations = @post.translations.where("translations.words=?", params[:words], params[:end]).joins("LEFT OUTER JOIN vocabularies ON vocabularies.translation_id = translations.id AND vocabularies.user_id = 6").select("translations.*, vocabularies.user_id as learner_id")

Please help me out. Thank you.

2
  • I think you can reduce the query to something like this @post.translations.joins(:vocabularies).where(words: params[:words] + params[:end], vocabularies: {user_id: 6}) and add the select at the end Commented Sep 7, 2014 at 14:13
  • I think I'm OK with joins command but I had trouble with select commands, it got error SQLException: near "*": syntax error Commented Sep 8, 2014 at 5:08

1 Answer 1

1

You need to construct your where statement like:

where("translations.words = ? OR translations.words = ?", params[:words], params[:end])

So your query will become:

@translations = @post.translations.where("translations.words = ? OR translations.words = ?", params[:words], params[:end]).joins("LEFT OUTER JOIN vocabularies ON vocabularies.translation_id = translations.id AND vocabularies.user_id = 6").select("translations.*, vocabularies.user_id learner_id")
Sign up to request clarification or add additional context in comments.

5 Comments

I changed but it get errors SQLite3::SQLException: near "*": syntax error @post.translations.joins(:vocabularies).where("translations.start=? AND translations.end=?", params[:start], params[:end]).select("translations.*","vocabularies.user_id AS learner_id")
Dear RAJ, I tried both, even with select("translations.*") and it still got error SQLException: near "*": syntax error: SELECT COUNT(translations.*, vocabularies.user_id learner_id)
everything was fine until I add select command, it works with .select("translations.id") but fails with `.select("translations.*")
Well, I think the problem is when i add an option .select(), it refers to COUNT() command in SQL
I'm sorry, it seems to be an error because after that, I add a command @translations.any? and if I remove this, everything works perfect, thanks for helping me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.