rails-activerecord Questions
2
Solved
I have a fat multi-tenant Rails app on Apache + mod_passenger that outputs product prices from a PostgreSQL table as follows:
Table "public.products"
Column | Type
id | bigint
name | character var...
Nursemaid asked 27/9, 2018 at 13:18
3
Solved
I'm using Rails polymorphic associations, that way some models have many cash_histories children, like this:
has_many :cash_histories, as: :cashable
But when I try to delete all cash histories f...
Epitomize asked 27/5, 2014 at 1:35
5
Solved
I have found that I have two "survey_id" columns in my schema and that's been causing some problems for me. Specifically I need to remove the second index as I don't want survey_id to be ...
Lipps asked 30/3, 2014 at 15:19
7
Solved
I'm terrible at naming and realize that there are a better set of names for my models in my Rails app. Is there any way to use a migration to rename a model and its corresponding table?
Atlantes asked 23/1, 2009 at 0:25
12
Solved
I currently have a model Attend that will have a status column, and this status column will only have a few values for it. STATUS_OPTIONS = {:yes, :no, :maybe}
1) I am not sure how i can validate ...
Solarize asked 16/11, 2011 at 5:2
3
Solved
Can I use a jsonb field as a foreign_key in a belongs_to association? Something along the lines of:
belongs_to :product, class_name: "Product", foreign_key: "data ->'product_id'"
Malaguena asked 1/10, 2015 at 13:46
29
Solved
I have some models that have after_save callbacks. Usually that's fine, but in some situations, like when creating development data, I want to save the models without having the callbacks run. Is t...
Haldis asked 10/3, 2009 at 23:52
3
Solved
I'm trying to find the best way to do a Postgres query with Common Table Expressions in a Rails app, knowing that apparently ActiveRecord doesn't support CTEs.
I have a table called user_activity_...
Kabyle asked 15/12, 2014 at 19:46
4
So I'm working with a colleague who added some additional migration files and per normal procedure once I pulled their version I ran rails db:migrate. I end up getting the following errors:
Index...
Gardal asked 25/10, 2017 at 15:14
6
Solved
I have an Active Record model that contains attributes: expiry_date. How do I go about validating it such that it is after today(present date at that time)? I am totally new to Rails and ruby and I...
Stoicism asked 14/12, 2011 at 22:34
5
Solved
I want to have a default value for my enum field to prevent it from being nil. I did the following:
# db/schema.rb
create_table "templates", force: :cascade do |t|
t.integer "status"
end
# app/m...
Scruple asked 31/1, 2016 at 9:11
24
Solved
I need to add timestamps (created_at & updated_at) to an existing table. I tried the following code but it didn't work.
class AddTimestampsToUser < ActiveRecord::Migration
def change_table...
Desmarais asked 25/9, 2011 at 1:5
28
Solved
I'm in need of getting a random record from a table via ActiveRecord. I've followed the example from Jamis Buck from 2006.
However, I've also come across another way via a Google search (can't att...
Exemplum asked 2/5, 2010 at 2:11
2
Solved
I am unable to use model.update_attribute on an attribute that is mounted by a carrierwave uploader. The SQL statement wont accept the value and adds NULL to the placeholder. If I remove the mount_...
Caban asked 23/9, 2013 at 0:6
1
Expressions like the following are common in Rails:
@project = Project.find params[:id] # example 1
@project = current_user.projects.find params[:project_id] # example 2
However I realize that ...
Schwann asked 1/9, 2016 at 10:14
5
Solved
Is there any way I can validate a single attribute in ActiveRecord?
Something like:
ac_object.valid?(attribute_name)
Method asked 26/1, 2011 at 12:33
7
Solved
I've been battling this guy for a while and have done all the Googlies on it (here, here, and many equally-unhelpful others) but to no avail.
The official error is this, called on the first line o...
Pentomic asked 16/9, 2018 at 21:14
6
Solved
I'm trying to remove the commas from a field in a model. I want the user to type a number, i.e. 10,000 and that number should be stored in the database as 10000. I was hoping that I could do some m...
Repossess asked 19/5, 2009 at 16:44
5
Because of this security advisory serialized attributes need to use YAML.safe_load with a safe list of allowed Classes.
The problem I am having is that I want to use a custom class (Foo::Bar) and i...
Yoko asked 5/10, 2022 at 9:5
13
Solved
I've been debugging this strange problem of Rails giving me "Unknown primary key for table...", even when the table's ID is there.
I've copied the database from one heroku app to another, on the o...
Scheider asked 5/8, 2013 at 10:39
3
Solved
I’m using Rails 4.2.7. I want to create a migration that enables an extension, but only if that extension doesn’t exist in the host environment in which I’m running. I have created
class EnableUui...
Vespid asked 29/8, 2016 at 19:27
13
Solved
I am new to rails. What I see that there are a lot of ways to find a record:
find_by_<columnname>(<columnvalue>)
find(:first, :conditions => { <columnname> => <columnva...
Kirman asked 22/6, 2012 at 17:58
1
Solved
Common Table Expression is a fairly common practice in different RDBMS (PostgreSQL, MySQL, Oracle, SQLite3 etc.) to perform the same calculation multiple times over across multiple query components...
Ariannearianrhod asked 9/2, 2023 at 1:16
2
My understanding is that all migrations are wrapped in a transaction, meaning, if a change for 1 row, like adding null: false, in a table fails, every other row will fail. When would you not want t...
Sorbose asked 6/9, 2017 at 4:30
3
Solved
This has been bugging me for a while...
How can I string interpolate a datetime in Rails ActiveRecord queries?
# Works, but supeh ugleh:
Model.where("created_at >= ?", Time.now - 5.days)
# Ho...
Gyatt asked 17/3, 2012 at 7:11
1 Next >
© 2022 - 2024 — McMap. All rights reserved.