Data in Database not reflected by Rails calls - ruby-on-rails

This is the strangest bug I've ever encountered
I am using the Best In Place gem in my Rails app in order to allow in-place editing of a page title. The in-place editing works, and the new title gets changed in the database, but when I refresh the page, it reverts back to the old title. I don't even understand where it's getting the old title from, since it's not stored in the database anymore.
When the page is created, it is automatically given the title "Untitled Page". When I change the title to say, "Title", and look at the row for the page in the DB with the postgresql admin program, it does indeed change to "Title". But when I run Page.find(1).title in the rails console, it returns "Untitled Page"
How could this be?!

Is your app running in production mode?
The PG response and console response cannot be different unless they are being executed on two different databases.
Check that your database config is using the same database as the one you are manually connecting to when browsing PG.
Ensure that when you run the rails console you specify the environment (in case your default is not what you are running on):
$ rails c production
$ rails c development
If both of the above don't help, please post the log snippet of this action. Might be that the transaction is being rolled back. If you are using .save or .update_attribute without the "!" then this won't throw an error. This is highly unlikely though, since you are saying that the database has updated data.

Fixed it. The reason was that I was using after_initialize to set the automatic properties, which gets called whenever the ActiveRecord object gets initialized. What I really wanted was after_create. I use after_create to call a function called set_properties, where I set things like self.title, and then at the end of the function, I call self.save, which is required for the properties to get saved into the DB.

Related

Reload! rails app without closing rails console or objects instantiated within it?

I am using Rails Console to help me muck around with an object instance running a Watir browser instance.
I've had to navigate via watir through various forms to get to where I want to thus I don't want to have to recreate the object and browser instance everytime I want to see the effects of changed code I'm wriring.
reload! doesn't seem to do the trick for me, confirmed for e.g. through changing proposed browser field text input in the code, calling reload! in the active rails console session and calling a method, the old text still used from before the reload!.
Is there a way to reload so that I can use uodated code on the already created objects in the already running rails console session?
Thanks

Showing INSERT and UPDATE statements for Rails server?

I apparently haven't set up my associations properly; records aren't being saved out to where I thought they would be. Checking the output on the Rails server (rails s), I see lots of SELECT statements but no INSERT or UPDATE statements.
Is there a way to view these types of queries with the rails server? Ideally I would like a solution that doesn't involve installing third party components... but if that is the only way, I might consider it after I throw a tantrum.
If you did not change your configuration, all SQL statements appear in your server console by default.
3 things you can do:
1- You can check your config/environmets/development.rb file if there is a config.log_level statement. And eventually set it to :debug:
config.log_level = :debug
2- Also be sure your rails server runs in development, you can see that in the first lines in the console, or add RAILS_ENV=development in your rails s command
3- Place some were in your code (preferably in top of an entry function of your controller) User.first.touch (or using any other model of your choice) this is to confirm the UPDATE statement is correctly appearing in the logs

Ruby on Rails Database Deployment with Gerrit

I'm considering using Ruby on Rails for my next project. Understanding the deployment of a rails website is easy enough to understand (sounds like I'll be using Phusion Passenger)
But now I'm trying to figure out the database. I see a lot about "database migrations", which allow me to update the database using ruby code. I also see that I'm allowed to create both an up and down variant of these migrations.
However, I can only fathom how this works cleanly in a single direction. Imagine if I suddenly say "The color column cannot be null". So, the up will make it required and give all NULL entries a default value. But what will the down do? If you care about it being identical to how it started, you can't just set the default values back to NULL.
This doesn't really matter much for releases to production. That will likely just be done in a single direction (in the up direction). However, I want to use Gerrit for code reviews as well as setting up a bot to run a build before allowing check-ins...
So how could that work? From one code review to the next, the build server will check out the new set of code, and run the migrations? But when this happens, it won't even retain the migration code from before, so how could it run the down steps? As an simpler example, I do not see how I could check out an old version of the code and "db migrate" backwards.
Yes, you can't check out an old version of the code and then run a down migration from a newer version of the code. You would need to run the down migration before rolling back to the older code.
There are many, many cases where a down migration is just not practical or possible. That's not necessarily a bad thing. It just means that you have defined a 'point of no return', where you won't be able to restore your database to an earlier state.
Migrations like creating a table or adding a column are easily reversed by simply destroying that table or removing that column. However, if you are doing something more complex, such as adding default values or moving data around, then you can tell Rails that it's not possible to reverse this migration:
def down
raise ActiveRecord::IrreversibleMigration
end
I would recommend that Gerrit should never assume anything about the database. It should start with a fresh database each time a new version is deployed, and run db:migrate to run all your migrations. You can use gems like factory_girl to populate your app with demo data for testing purposes.

Troubleshooting: Rails can't save to database in production?

I could use a little help trouble shooting this problem.
When using the app to create a new record nothing is being saved to the database.
There are no visible errors presented.
Dropping to the command line, and using the console with the same production environment, I can create a new object and save it (I have to bypass validations). If I look in mysql database I can see the record that I created from the console.
App works fine locally.
Any thoughts on what might be the problem?
Rails 2.0.2
Sounds like a validation error.
In your controller, try using save! (with the bang) to see if will throw a meaningful error.
I am not sure what code you have in the controller, but this might help show the problem
if my_object.save
log.debug 'object saved correctly'
else
log.debug my_object.errors.full_messages
end
Good luck, if this doesn't help. Try posting the relevant controller and model code.
Have you double checked that the request (with params, etc) works correctly in development?
If not, perhaps looking at the production log file will tell you where the request was routed (e.g. which controller and action, and with which parameters).

Why is attribute not saving in Rails?

I've added an "account" variable to a Rails app I'm running, and tested in the development environment with a mongrel server. Everything worked fine. I set my environment to production and use our Apache server, and suddenly nothing works. After a lot of debugging, I've found that the account variable is succesfullying being SET in my methods, but it's not SAVING (that is, once it gets out of the method that sets it, it's nil). I can call save or save! as many times as I want, and it's still not being set.
The attribute is accessible, and I'm not seeing any errors in the logs... It's just not saving.
Any idea what's going on?
-Jenny
Ah, I migrated to dev, but not production. I didn't think it could be the migrations, because if it were, I reasoned, I wouldn't be able to access #video.account, or whatever, because I would get a "method does not exist" error (which is what I was getting before I migrated in dev).
A bit more information to help you out on why that happened:
Check out the file db/schema.rb - it contains a Ruby representation of your database, updated on each migration. Models in Rails base their attributes on this file.
So when you migrate in development mode, the schema file is updated. When you move to production mode, that file is kept, and Rails doesn't know that the columns you are trying to assign don't exist. As such, the object you update accepts the attribute assignment, sends the query, and moves on - not noticing that the attribute didn't really save.
That's my understanding of it - hope this helps you in your quest!

Resources