Rails table doesn't exist after create model from existing table - ruby

I had an existing table named boss_name and I would like to create a rails model for it.
I used "rails generate bossname" to created the model and added the
self.table_name = "boss_name"
inside the class.
After the model had been successfully generated, I tried to start up rails console and trying to query the table.
Bossname.first give me the first value from boss_name table without problem.
rails console worked fine but when I'm running rspec for bossname_spec.rb, I had error which say "Bossname(Table doesn't exist)".
I hope anyone can tell me why it work for rails console and doesn't work for the application. Any hint on how to make it work on the application too is really appreciated.

That indicates that the boss_name table exists in your development database, but not in your test database. You can copy your current development database schema into your test database like this:
rake db:schema:dump
RAILS_ENV=test rake db:setup

Related

Add columns to a table managed by Sequel Model

I'm using Sequel::Model. My model has a set_schema block and a create_table call, so the database is completely managed by the model. The database is sqlite.
I'm trying to add columns to the database and I can't find a way to. Adding fields to the schema has no effect. Adding a migration or an alter_table call doesn't do anything. I've asked on IRC and read the docs. I can't find an example of anyone doing this, but it seems simple.
How do I add a column/field to a Sequel Model?
I did a bunch of research and talked to Jeremy Evans. I was going about this wrong. The right way to do this was to remove my schema plugin code and create_table block and move to using migrations. The steps I went through were:
Remove the schema code (create_table, set_schema) from my modesl
Dump the schema from my current sqlite data files into initial migrations into migration files via the sequel -d command
Create a new migration that adds the columns I need via add_column calls in an alter_table block in a change block
Apply the migrations via the sequel -m command
Create rake tasks to run the migrations and hook those tasks into my deploy tasks

Re rake after you edit a model?

I am just starting my first ruby project. I followed tutorial and built a blog. I thought next i would add a commenting system to extend the project, I just added has_many :comments to my post model. In the tutorial after we built the model we raked the database. Im not entirely sure what it does, but it seems fairly important. Is this something at I will need to do again and any time i update a model? I am using the gem 'sqlite3'
thanks
Every time you create a new model (database table) you got to run the migration, which creates the table in the database. Whenever you make any change to the db, you got to run migrations so as to make sure your db is in sync with your latest changes.
Everytime, you create/edit a model(database table) in rails, you should write a migration for the same. The migration will make the necessary changes to your database once it is run using "rake db:migrate" command.
With the has_many :comments in your blog model. You will need to add a migration to create the comments table, with blog_id as the foreign key, assuming your blog model is called 'blog'.

Using Padrino and DataMapper to access an existing Database

I'm migrating a sinatra app I have that acts as a backend UI for our DNS database. I've already got the DM configs in the sinatra app but want to migrate it to padrino so I can make it cleaner and easier to read, but also because I want to play around to padrino. If I just generate a new model, can I perform the datamapper mapping in that model, including specifying the db application and get away with doing that instead of using a generator?
What do I need to do to be able to access models on a different database, ideally without damaging that data base (read only)
Right so you actually can do this I found out with a bit of trial and error. Specify the datamapper database source in the config/boot.rb there's a section labelled Padrino.after_load, you'll want to add in your new DataMapper source here
DataMapper.setup(:myalternatedatasource, "MY_ALTERNATE_DB_URL
Then in your model file you'll want to specify
def self.default_repository_name
:myalternatedatasource
end
And it'll all work as intended!

padrino-gen migration create_indexes is not working

I am trying to use mongoid_fulltext and can't seem to get the migrations to create the indexes needed. When I try:
padrino-gen migration create_indexes
I get a result
apply orms/mongoid
But when I empty out a document and rebuild it, the new indexes do not show up. Am I doing something wrong?
That isn't the correct way to execute the command. It is something more like padrino rake mongoid:create_indexes. If you generated your project with mongoid as the ORM component, that should work. Run padrino rake -T to see the available tasks.

db:migrate order in Spree

I'm using spree and created a new payment gateway extension. The problem is, my newly created payment gateway gets created first before the core payment gateway of spree. Here's the error message.
doesn't exist: SHOW FIELDS FROM gateway_options
I've had the same problem. Basically, there's a way to define the order in which extensions are loaded but not when their migrations are ran.
config.extensions = [:all, :site]
More info here.
The way I do it, is simply by renaming the "db" folder of the extensions' migrations needing to be ran later. When the others have ran, I rename it back to its original name and run the migrations again. Dirty, but it works.
There could probably be a way to make a rake task and automate this.

Resources