Rails migration issue column - ruby-on-rails

I have simple migration for add column
class AddSeatGroupForFee < ActiveRecord::Migration
def up
add_column :fees, :seat_group_id, :integer
end
end
my Fee model
belongs_to :seat_group
my SeatGroup
has_many :fees
and when i want to use this column in next migration
i haven't this column
when i stop with binding.pry
seat_group_id
should be presents
Fee
=> Fee(id: integer, code: string, created_at: datetime, updated_at: datetime)
and Fee hasn't seat_group_id column here
world_business = SeatGroup.create({name: '(name)', airline_code: 'code'})
world_business.fees.where(code: 'W-BUSINESS') => error here
why it happens?
rails 4

Fee.reset_column_information
fixed my problem

Related

Problems with money-rails in Ruby

I was following a tutorial to use money-rails in a new project.
Here is my migration file:
class AddFieldsToPlan < ActiveRecord::Migration[5.1]
def change
add_column :plans, :payment_gateway_plan_identifier, :string
add_column :plans, :price, :integer
add_column :plans, :interval, :integer
add_column :plans, :interval_count,:integer
add_column :plans, :status,:integer
remove_column :plans, :amount
remove_column :plans, :payment_frequency
end
end
And my model:
class Plan < ApplicationRecord
enum status: {inactive: 0, active: 1}
enum interval: {day: 0, week: 1, month: 2, year: 3}
monetize :price_cents
def end_date_from(date = nil)
date ||= Date.current.to_date
interval_count.send(interval).from_now(date)
end
end
I read all the API specification of money-rails but doesnt understand well I guess.
If I run the rails console, and do a Plan.last.price it shows me this error:
.3.4 :001 > Plan.last.price
Plan Load (2.6ms) SELECT "plans".* FROM "plans" ORDER BY "plans"."id" DESC LIMIT $1 [["LIMIT", 1]]
NoMethodError: undefined method `price_cents' for #<Plan:0x007f8ca807f8f0>
Did you mean? price_cents=
from (irb):1
What Im doing wrong here? How can I set up a value for this price attribute?
Thanks
Look at the tutorial for `money-rails' you'll see the migration they recommend is
add_monetize :products, :price # Rails 4x and above
That actually creates an integer field called price_cents in the model.
You need another migration to remove price and then use the above line to add the price_cents to the table.

Remove and edit attributes in a roby on rails model

So I made a mistake when generating the model.
I need to remove the "integer: string" and I want to the movie_id to be a integer not a string. Thanks
Review(id: integer, rating: integer, comment: text, created_at: datetime, updated_at: datetime, user_id: integer, movie_id: string, integer: string)
Any help will be appreciated!
You can use the command.
rake db:rollback
then go to the migration file.
remove
t.string :integer
and update
t.integer :movie_id
save and again run command.
rake db:migrate
You can also add another migration to do this. create a migration file. write following code in it
def change
remove_column :reviews, :integer, :string
change_column :reviews, :movie_id, :integer
end
and run
rake db:migragte
You can write another migration file for that, in which you can remove the column you want and change the type of the other. In your new migration file write:
def change
remove_column :reviews, :integer, :string
change_column :reviews, :movie_id, :integer
end

Why Active Record storing integer value in the format of BigDecimal notation?

I am a newbie. observe following screen shots, I have a model Books with following columns.
class CreateBooks < ActiveRecord::Migration
def change
create_table :books do |t|
t.string :title
t.text :author
t.integer :price
t.integer :book_id
end
end
end
Later I add a column to existing table like following
class AddBookIdToBooks < ActiveRecord::Migration
def change
add_column :books, :book_id, :integer
end
end
I add a record into table Book using form helpers. But it is storing book_id value in the format of BigDecimal notation instead of integer.
Why it is storing like that?? Thanks in advance.
observe following screen shot here
2.2.2 :023 > Book.all
Book Load (0.4ms) SELECT "books".* FROM "books"
=> #<ActiveRecord::Relation [#<Book id: 7, title: "Wings Of Fire", author: "kalam's", price: 120, created_at: "2015-12-09 09:57:10", updated_at: "2015-12-09 09:57:10", book_id: #<BigDecimal:4f39ea0,'0.1001E4',9(27)>>]>
You don't need to add an id for your model. Rails does it for you. When you add a column called "model"_id, Rails infers that it will be a reference to other table (association).

How to arrange the order of columns in a database of a Rails application?

I recently added a :title column using Rails migration:
class AddTitleToMicroposts < ActiveRecord::Migration
def change
add_column :microposts, :title, :string
end
end
I noticed that it appear at the end when I do user.microposts: in the console:
=> [#<Micropost id: 1, content: "test", user_id: 1, created_at: "2012-01-25 15:34:30", updated_at: "2012-01-25 15:34:30", title: nil>]
Is there any way of arranging the order of the title columns? Say, placing it right before the :content column?
There is an :after option to insert the columns (no :before option unfortunately)
class AddTitleToMicroposts < ActiveRecord::Migration
def change
add_column :microposts, :title, :string, :after => :content
end
end
Rearrange them in the schema and do rake db:schema: load

Rails 2.3.8 Association Problem has_many belongs_to

I'm new to Rails. I have two models, Person and Day.
class Person < ActiveRecord::Base
has_many :days
end
class Day < ActiveRecord::Base
belongs_to :person
has_many :runs
end
When I try to access #person.days I get an SQL error:
$ script/consoleLoading development environment (Rails 2.3.8)
ree-1.8.7-2010.02 > #person = Person.first
=> #<Person id: 1, first_name: "John", last_name: "Smith", created_at: "2010-08-29 14:05:50", updated_at: "2010-08-29 14:05:50"> ree-1.8.7-2010.02
> #person.days
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: days.person_id: SELECT * FROM "days" WHERE ("days".person_id = 1)
I setup the association between the two before running any migrations, so I don't see why this has not been setup correctly.
Any suggestions?
Telling your model about the association doesn't set up the foreign key in the database - you need to create an explicit migration to add a foreign key to whichever table is appropriate.
For this I'd suggest:
script/generate migration add_person_id_to_days person_id:integer
then take a look at the migration file it creates for you to check it's ok, it should be something like this:
class AddPersonIdToDays < ActiveRecord::Migration
def self.up
add_column :days, :person_id, :integer
end
def self.down
remove_column :days, :person_id
end
end
Run that and try the association again?

Resources