rails, activerecord, get current connection specification

Similar to the way that you can call connection on a model, you can make a call to connection on ActiveRecord::Base. ActiveRecord::Base.connection_config Look at the docs for ActiveRecord::Base, as there are other methods that allow you to get/set attributes about the connection. Update The connection_config became deprecated and will be removed from Rails 6.2. connection_db_config … Read more

Rails: includes with polymorphic association

Edit 2: I’m now using rails 4.2 and eager loading polymorphism is now a feature 🙂 Edit: This seemed to work in the console, but for some reason, my suggestion of use with the partials below still generates N+1 Query Stack warnings with the bullet gem. I need to investigate… Ok, I found the solution … Read more

Rails idiom to avoid duplicates in has_many :through

As long as the appended role is an ActiveRecord object, what you are doing: user.roles << role Should de-duplicate automatically for :has_many associations. For has_many :through, try: class User has_many :roles, :through => :user_roles do def <<(new_item) super( Array(new_item) – proxy_association.owner.roles ) end end end if super doesn’t work, you may need to set up … Read more

Getting error indicating number is “out of range for ActiveRecord::Type::Integer with limit 4” when attempting to save large(ish) integer value

I ran into the same problem, and the answer above gave me a clue on how I fixed mine. I propose my answer a little detailed to solving the problem. You can do this by setting limit on your table column. Hack/Steps Run a migration to change your table column. e.g. rails generate migration change_integer_limit_in_your_table … Read more

How to combine two conditions in a where clause?

solution 1: ( I prefer , more easy to understand) Just write it like raw SQL: Comment.where( “(user_id > ? AND user_id < ? ) OR (created_at > ? AND created_at < ?)”, 100, 200, “2022-06-01”, “2022-06-05”) solution 2: Comment. where(:created_at => time_range). where(“user_id is not in (?)”,[user_ids]) which will produce SQL like : select … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)