Weird Rails migration / schema.rb issue - ruby-on-rails

A while back I ran the following migration:
class CreatePipelineSpecs < ActiveRecord::Migration
def change
create_table :pipeline_specs do |t|
t.integer :id_no
t.string :od
t.string :wt
t.string :material
t.string :spec_type
t.string :spec_grade
t.string :mop
t.string :stress_level
t.string :joints
t.text :notes
t.string :ip
t.references :pipeline, index: true, foreign_key: false
t.timestamps null: false
end
add_index :pipeline_specs, :id_no
end
end
I am not sure what has happened now but every time I run rake db:migrate the scheme.rb file gets updated with:
create_table "pipeline_specs", force: :cascade do |t|
t.integer "id_no"
t.string "od"
t.string "wt"
t.string "material"
t.string "spec_type"
t.string "spec_grade"
t.string "mop"
t.string "stress_level"
t.string "joints"
t.text "notes"
t.string "ip"
t.integer "pipelines_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "pipeline_specs", ["id_no"], name: "index_pipeline_specs_on_id_no", using: :btree
add_index "pipeline_specs", ["pipelines_id"], name: "index_pipeline_specs_on_pipelines_id", using: :btree
Note the plural pipelines_id. The actual database tables (dev, production etc.) are all pipeline_id which is correct as the reference table is Pipeline. So I add a new unrelated migration and the schema.rb gets updated and these get changed back to plural again after I change them back. If I forget to change them when I run tests everything breaks as the wrong schema gets loaded into the test environment.
I am at a loss here. I am I missing something obvious here or is there some hidden migration schema table etc.
The only think I can this of is when I made the original migration I used pipelines:references vs pipeline:references, then fixed my mistake, and then cleaned up the migrations before committing this and deploying it.
Any ideas here one why this is happening and how to fix it once and for all?
UPDATE
Here are my three related models:
irb(main):031:0> Pipeline
=> Pipeline(id: integer, licence: string, company: string, company_id: integer, ba_code: string, substance_code: string, substance: string, h2s: string, partial_pressure: string, notes: text, created_at: datetime, updated_at: datetime, slug: string)
irb(main):032:0> PipelineSpec
=> PipelineSpec(id: integer, id_no: integer, od: string, wt: string, material: string, spec_type: string, spec_grade: string, mop: string, stress_level: string, joints: string, notes: text, ip: string, pipeline_id: integer, created_at: datetime, updated_at: datetime, slug: string)
irb(main):033:0> PipelineSegment
=> PipelineSegment(id: integer, line: integer, lsd_from: integer, sec_from: integer, twp_from: integer, rge_from: integer, m_from: integer, fc_from: string, lsd_to: integer, sec_to: integer, twp_to: integer, rge_to: integer, m_to: integer, fc_to: string, length: string, aasm_state: string, state_comment: string, state_user_id: integer, aasm_date: datetime, env: string, volume: string, notes: text, pipeline_id: integer, pipeline_spec_id: integer, created_at: datetime, updated_at: datetime, slug: string)
Pipeline has_many PipelineSpec and PipelineSegment. PipelineSegment has_one PipelineSpec.
UPDATE 2
Checked my test environment schema - it's fine. Ran rake db:migrate and again the schema.rb gets updated. Run the tests again and get gobs of:
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "pipeline_id" of relation "pipeline_specs" does not exist
LINE 1: ..., "mop", "stress_level", "joints", "notes", "ip", "pipeline_...
^
: INSERT INTO "pipeline_specs" ("id", "id_no", "od", "wt", "material", "spec_type", "spec_grade", "mop", "stress_level", "joints", "notes", "ip", "pipeline_id", "created_at", "updated_at") VALUES (1, 1, '88.9', '3.18', 'S', 'Z245.1', '359 2', '9930', '25', 'W', 'MyText', 'U', 1, '2017-04-24 03:47:26', '2017-04-24 03:47:26')
as the fixtures try to load into the incorrect test schema that was just loaded at test time.

The schema is recreated from the actual state of your database after each migrations run. So I guess the pipelines_id must indeed exist somewhere in your db or at least Rails must think it exists in the db. I would do the following to narrow the issue down:
Run rake db:schema:dump SCHEMA=myschema.rb - this will generate a differently-named schema file manually. This is the same rake task that is also run after the migrations. I expect that the new schema will contain the plural pipelines_id column, too.
Then I'd take a look in the log/development.log (or whatever environment you are experiencing the issue in). You should see SELECT schema_migrations.* FROM schema_migrations and a bunch of further queries for showing structure of each table in your database there. Find the query that deals with the pipeline_specs table and run it manually in the db console to see what you precisely get. My expectation is that you'll either:
see the plural pipelines_ids there too - that would prove the column indeed exists in the db
or not - in which case I would restart everything I could in rails just to be sure there is no caching involved anywhere - specifically I'd restart spring with spring stop. Also I'd check (don't know Postgres that well to tell for sure) whether there might be any caching involved on the Postgres side. Then I'd try again.

I think that because the migration you deleted did't run on test environment, try reset the test database by:
RAILS_ENV=test rake db:migrate:reset

Try to specify a foregein_key in model PipelineSpec to pipelines_id
class PipelineSpec < ActiveRecord::Base
belongs_to :pipeline, foreign_key: :pipelines_id
end

Related

Ruby on Rails Testing - ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: malformed array literal:

I have a project that is running ok on production and development and mostly on many tests, but with one model there's no way to run the tests.
Model:
create_table "cities", force: :cascade do |t|
t.string "city"
t.string "cp", array: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "primary_cp"
end
(Note that "cp" attribute is an Array and I'm using Postgres on the project)
(I'm NOT setting "serialize: cp, Array" on the model)
But when I run rails test/models/city_test.rb what I get is
Error:
CityTest#test_the_truth:
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR:
malformed array literal: "MyString"
LINE 28: ...d_at") VALUES (980190962, 'MyString', 'MyString',
'MyString'...
^
DETAIL: Array value must start with "{" or dimension information.
: DELETE FROM "homes";
DELETE FROM "categories";
DELETE FROM "volunteers";
DELETE FROM "donations";
I've already searched for other solutions here but it doesn't fix the problem. What I'm guessing is that it is something related to the YAML serialization that the test environment does, but I'm not quite sure.
I have no problem with development and production environments at all, it only happens on test env.
For those who came up with same situation. the problem was with the declaration of the test fixtures. An array in YAML must be declared this way:
one:
city: Premià de Mar
cp:
- 08400
- 08401
two:
city: Mataró
cp:
- 08300
- 08301
Where "cp" is the array. Setting up the array properly fixes the issue.

postgreSQL date records only persisting up to a certain point in time in Rails

I am seeding some simple data in my rails program that is using a postgres database.
Currently, it is only persisting certain dates to the database. Other times, it is showing up as null in my API, which is very odd. I will post a picture of my database, and the seeded data, as well as my JSON API.
Here is my table:
create_table "pickup_deliveries", force: :cascade do |t|
t.date "pickup_date"
t.text "pickup_location"
t.money "rate", scale: 2
t.date "delivery_date"
t.text "delivery_location"
t.boolean "local"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "loaded_miles", default: 0
t.integer "deadhead_miles", default: 0
end
Here is my seeded data:
PickupDelivery.create(:pickup_date => '05-10-2019', :pickup_location => 'Kansas City, MO', :rate => '550.00', :delivery_date => '05-11-2019', :delivery_location => 'Wichita, KS', :local => false, :loaded_miles => '550', :deadhead_miles =>'230');
PickupDelivery.create(:pickup_date => '05-20-2019', :pickup_location => 'Kansas City, MO', :rate => '550.00', :delivery_date => '05-25-2019', :delivery_location => 'Wichita, KS', :local => false, :loaded_miles => '550', :deadhead_miles =>'230');
Here is the persisted data in my JSON API:
[
{
id: 1,
pickup_date: "2019-10-05",
pickup_location: "Kansas City, MO",
rate: "550.0",
delivery_date: "2019-11-05",
delivery_location: "Wichita, KS",
local: false,
created_at: "2019-05-09T16:14:35.312Z",
updated_at: "2019-05-09T16:14:35.312Z",
loaded_miles: 550,
deadhead_miles: 230
},
{
id: 2,
pickup_date: null,
pickup_location: "Kansas City, MO",
rate: "550.0",
delivery_date: null,
delivery_location: "Wichita, KS",
local: false,
created_at: "2019-05-09T16:14:35.319Z",
updated_at: "2019-05-09T16:14:35.319Z",
loaded_miles: 550,
deadhead_miles: 230
}
]
As you can see, the dates were both inputted in the same format, but only one came out as intended while the other came out as null
Thanks for reading.
The reason is that you are passing an invalid format in the second record
# Record 1
:delivery_date => '05-11-2019' # dd-mm-yyyy
# to
delivery_date: "2019-11-05"
# Record 2
:delivery_date => '05-25-2019' # dd-mm-yyyy
# Invalud since there is no month 25
so it becomes nil
This date is being parsed as dd-mm-yyyy
The date you are passing in the second record is invalid due to the month
I suggest you pass the date in this format 'dd-mm-yyyy' - '20-05-2019'
NOTE: Even in your first record the date in being parsed as 05 the day, 11 as the month and 2019 as the year

ActiveRecord returns one ghost null column for an id less table

I have only 9 columns in a table.
duggout=# \d fantasy_selections
Table "public.fantasy_selections"
Column | Type | Modifiers
-----------------+-----------------------------+-----------
user_id | integer |
match_id | integer |
player_id | integer |
points | integer |
is_captain | boolean |
is_vice_captain | boolean |
is_star_player | boolean |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
Indexes:
"index_fantasy_selections_on_user_id_and_match_id_and_player_id" UNIQUE, btree (user_id, match_id, player_id)
But, when I query the table, I get 10 columns with 1 column having a null key. This creates a problem during JSON serialization. Check the last value in the following hash. It is "nil=>nil". For a new record, I do not see the null column.
In a fresh Rails console:
☁ duggout [master] ⚡ rails c
Loading development environment (Rails 4.1.4)
> f = FantasySelection.new
=> #<FantasySelection user_id: nil, match_id: nil, player_id: nil, points: nil, is_captain: nil, is_vice_captain: nil, is_star_player: nil, created_at: nil, updated_at: nil>
> f.attributes
=> {"user_id"=>nil, "match_id"=>nil, "player_id"=>nil, "points"=>nil, "is_captain"=>nil, "is_vice_captain"=>nil, "is_star_player"=>nil, "created_at"=>nil, "updated_at"=>nil, nil=>nil}
> FantasySelection.selection_of(99,6158).first.attributes
FantasySelection Load (0.4ms) SELECT "fantasy_selections".* FROM "fantasy_selections" WHERE "fantasy_selections"."user_id" = 99 AND "fantasy_selections"."match_id" = 6158 LIMIT 1
=> {"user_id"=>99, "match_id"=>6158, "player_id"=>1, "points"=>100, "is_captain"=>nil, "is_vice_captain"=>nil, "is_star_player"=>nil, "created_at"=>Sun, 25 Jan 2015 11:31:54 UTC +00:00, "updated_at"=>Sun, 25 Jan 2015 11:31:54 UTC +00:00, nil=>nil}
> FantasySelection.selection_of(99,6158).first.to_json
`TypeError: nil is not a symbol`
The model:
class FantasySelection < ActiveRecord::Base
belongs_to :user
belongs_to :match
belongs_to :player
scope :selection_of, ->(user_id, match_id) { where(user_id: user_id, match_id: match_id) }
end
Schema.rb for this table:
create_table "fantasy_selections", id: false, force: true do |t|
t.integer "user_id"
t.integer "match_id"
t.integer "player_id"
t.integer "points"
t.boolean "is_captain"
t.boolean "is_vice_captain"
t.boolean "is_star_player"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "fantasy_selections", ["user_id", "match_id", "player_id"], name: "index_fantasy_selections_on_user_id_and_match_id_and_player_id", unique: true, using: :btree
Note that this table does not have a id column. It was created with id: false during create_table migration.
What could be the issue?
Would you believe me if I said the nil id had nothing to do with anything?
I tried to recreate the error and decided look deeper in the trace.
Tracing Stack
It's useful to trace the callstack more so I tried to recreate this error. I went something along the lines of ...
TypeError: nil is not a symbol
from /home/david/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.5/lib/active_model/serialization.rb:108:in `block in serializable_hash'
from /home/david/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.5/lib/active_model/serialization.rb:108:in `each'
from /home/david/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.5/lib/active_model/serialization.rb:108:in `serializable_hash'
This suggested to me a problem with the object that you are asking to convert to JSON.
In your example, FantasySelection.selection_of(99,6158).first.to_json returns an error, but if in rails console, you save it to an instance:
a = FantasySelection.selection_of(99,6158).first.to_json
then a.to_json things work fine.
Explanation?
While still in your console, try
FantasySelection.selection_of(99,6158).first.class
then, try
FantasySelection.selection_of(99,6158).first.attributes.class
You will see that you can convert Hash to JSON, but not the first, which is just database result returned in your console.
I suspect this may not solve your problem (I am only addressing the rails console error you pointed to). I hope this link is useful:
rails 4 activerecord TypeError nil is not a symbol

Unset operation failing for MongoMapper model, cannot delete / remove key from model

We're on mongodb 2.0.0, mongo gem 1.4.1, mongo_mapper 0.9.2, rails 3.0.6.
We love MongoMapper, but we need helping resolving one nasty issue: we have a key carried over from some testing, but invoking obj.unset fails to do anything.
Specifically, we are trying to remove an "id" key (not "_id") because it's causing MM to treat obj.id as different from obj._id, which we don't want.
After clearing out the database, we ran these commands from a controller which does nothing else: (We also tried running the same code from the rails console, but it also fails.)
logger.info "#{Game.keys.keys.inspect}"
Game.unset({}, :id)
logger.info "#{Game.keys.keys.inspect}"
Game.unset(:id)
logger.info "#{Game.keys.keys.inspect}"
Output:
["jackpot", "players", "created_at", "puzzles", "ended_at", "player_index", "updated_at", "log", "_id", "id", "join_code", "puzzle_index"]
["jackpot", "players", "created_at", "puzzles", "ended_at", "player_index", "updated_at", "log", "_id", "id", "join_code", "puzzle_index"]
["jackpot", "players", "created_at", "puzzles", "ended_at", "player_index", "updated_at", "log", "_id", "id", "join_code", "puzzle_index"]
Current keys defined in our Game model:
key :players, Array, :default => []
key :player_index, Integer, :default => 0
key :puzzles, Array, :default => []
key :puzzle_index, Integer, :default => 0
key :join_code, String, :default => nil
key :jackpot, Integer, :default => 0
key :log, Array, :default => []
key :created_at, Time
key :updated_at, Time
key :ended_at, Time, :default => nil
Help?
Thanks!
It pains us to post the answer since this solidifies our status as "morons, idiots, fools, noobs, Jay Leno fans," but in case anyone else bumps into the same issue: while our model directory was clean in the dev environment, the model dir in the production environment contained old test files ... which contained an old model with the "id" key.
Obviously, removing the old files and the old models solved everything, though we're left with staggering bruises to our egos and to our heads (from excessive banging against the walls).

Viewing the contents of tables in schema.rb in rails

I'm sorry if this is a stupid question, but in my schema.rb I have several tables like
create_table "messages", :force => true do |t|
t.integer "user_id", :null => false
t.string "message", :null => false
t.datetime "created_at", :null => false
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
end
Is it possible to view the contents of each table i.e view each message and associated user id, message content, time created at, linked image, etc?
Thanks
A database schema represents the structure of the database, not the content of it.
If you want to access the content in your database, you would query it to do so. You can do that via command line clients (running $ rails dbconsole will try to open one for the configured database) or graphical tools like Sequel Pro (for MySQL on Mac OS X).
You can also get this through your Rails application by running $ rails console and then using the methods available through ActiveRecord (e.g. Post.all or User.where(:name => 'Billy').limit(5)).
You can use gem "annotate". It is very helpful for me.
For viewing the table structure on the rails console only need to run the table name eg If there is a table with name country. only run the 'Country '
2.4.4 :004 > Country
=> Country(id: integer, name: string, percentage: string, created_at: datetime, updated_at: datetime, is_sync: boolean)
2.4.4 :005 >

Resources