1

Hi I migrate an app from Rails 2 to Rails 7.04.

I don't see how to rewrite :

self.accepted_roles.find_all_by_name(role_name).any? { |role| role.users }

users = self.accepted_roles.find_all_by_name(role_name).collect { |role| role.users }

users.flatten.uniq if users

I think self.roles.where("name="role_name).find_each but .any? and .collect

I don't find Rails 2 documentation. Help is welcome

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
jean-luc
  • 7
  • 4
  • I would seriously consider if you want to do a re-write. Rails 1 is just so different from later versions and updating 6 major versions is going to be a very long and painful upgrade path. Unless its a big application it might take you significantly longer then a re-write. – max Dec 01 '22 at 13:39
  • `User.where(id: self.accepted_roles.joins(:users).where(name: role_name).select(User.arel_table[:id]).distinct)` should work. That being said I agree with @max Rails 2 -> Rails 7 is a massive jump and accordingly should be done incrementally which, if done correctly, will take a very long time. A rewrite and data migration is likely a more cleaner and more efficient path. – engineersmnky Dec 02 '22 at 18:05
  • Yes it is a massive jump. In begining, i want to jump to Rails 3 but it is not possible on Rehl 8. And it is very very difficult to have an OS version-Ruby -rails stable configuration. is "any?" = "distinct" ?? i don't know arel_table? – jean-luc Dec 04 '22 at 10:13

1 Answers1

0

I think that .where() is what you are looking for.

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
  • Not clear. I use .where() but how to translate . any? and .collect ? – jean-luc Dec 01 '22 at 12:50
  • For example: Book.where(category: "Ruby", author: "Jesus Castello") This returns books that match both category & author. If there are no matches it will return nill. – Themis Nikellis Dec 01 '22 at 13:00
  • You translate Book.find_all_by_category_and_author(ruby,author) but how to translate Book.find_all_by_category_and_author(ruby,author).any? How to translate Book.find_all_by_category_and_author(ruby,author).collect Any? and collect not important??? – jean-luc Dec 01 '22 at 13:26
  • If with ".any?" you want to find if there is any instance with these attributes then i have already answered. Use .where() which will return nill if there are no matches. – Themis Nikellis Dec 01 '22 at 13:41
  • So Book.find_all_by_category_and_author(ruby,author) or Book.find_all_by_category_and_author(ruby,author).any? is the same request. And with ".collect" ? – jean-luc Dec 01 '22 at 13:54
  • If with .collect you want to store the instances just write: variable = Tablename.Where(...) and the results will be stored in your variable. – Themis Nikellis Dec 01 '22 at 14:00
  • Sidenote: `where()` will never return `nil` it will always return an `ActiveRecord::Relation`. This object can have no members but it will never be `nil` – engineersmnky Dec 02 '22 at 18:01