Devise Gem - How do I clear the configuration values from cache - devise

I am using devise gem for my rails app. Whenever I try to update the settings like max. number of attempts, Password Regex, Lock Strategy etc. I am forced to restart the server, as otherwise the changes do not take effect. This implies that these values are being cached. What is an efficient way to remove cached values in devise?

Everything in app/config folder loads only once during server boot.
Trying to hack won't make your life easier.
(though I want to know if it is possible)

Related

Rails 3.2 ActiveRecord Session Store Not Working

I'm migrating my application from Rails 3.0.12 to 3.2. I use the active_record_store to work with my session variables, owing the size limits on the default cookie store. On the new version of Rails, however, the session variable is not getting set.
I can see that the cookie session_id is being set with a value, but I can look at the contents of the database and see no values being inserted when a session variable is supposed to be populated.
However, if I switch back to cookie store, it works fine. Is there anything I can check to find out what's going on?
I'm afraid the problem solved itself, perhaps in a Gem update that I performed in preparing to respond to the comments on this question. I had the Rails 3.2 version of my app on a separate Git branch, so I brought it back and merged it with my current development version.... and it all "just works".
I can't help but think this was a transient issue with either an older version of my code or a gem, but it's solved now, so I'll close this question.

Learning Ruby to use serverside to manage an existing database

I am very much a beginner to rails and I have a specific need for it. I wish to connect it to a flex app, and also use it to call a database.
For example when the following web page is visited:
?User=bob&id=4&lic=234
Take 'bob' and 4 and add them to an existing database table if the license number is valid and not already used. I don't want to use the standard database stuff, I will need to write my own SQL queries as well as have logic for checking other stuff to do with registering users etc.
It is already done with Coldfusion but I am having issues with it, and would like to have it work with Ruby instead.
But to start with, I just want the ability to call a web page (on localhost:3000) get the string after the URL "?User=bob&id=4&lic=234" in this case and output "User:bob id:4" to the screen. I want to learn what the code is doing rather than have the least lines of code solution. I would rather use as little of the rails framework to do this as this will help me learn the language. I won't be making the standard website so learning the rails framework won't be so useful to me.
Thanks for the comments, sinatra seems to be what I'm looking for, however I am having problems connecting with databases, mysql in this case:
I'm following this:
http://community.active.com/blogs/productdev/2011/02/28/using-activerecord-3-without-rails
Trying to use a Mysql database in this instance.
However I get this error:
LoadError: Please install the mysql adapter: gem install activerecord-mysql-adapter (193: %1 is not a valid Win32 application. - F:/Software/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so)
So I tried:
gem install activerecord-mysql-adapter
However:
gem could not be found
So I did:
install instead activerecord-jdbcmysql-adapter-1.2.2
However it still didn't work.
When this is working, how do you make arbitrary SQL calls?
Have a look at Thin or Sinatra and combine it with Active record. Using Active Record to handle your database and models is far superior to doing it all by yourself.
If you insist on doing it by yourself. You can use the mysql2 gem or postgresql gem directly

Rails: Best practice for handling development data

I have the following scenario:
I'm starting development of a long project (around 6 months) and I need to have some information on the database in order to test my features. The problem is that right now, I don't have the forms to insert this information (I will in the future) but I need the information loaded on the DB, what's the best way to handle this? Specially considering that once the app is complete, I won't need this process anymore.
As an example, lets say I have tasks that need to be categorized. I've begun working on the tasks, but I need to have some categories loaded on my db already.
I'm working with Rails 3.1 btw.
Thanks in advance!
Edit
About seeds:I've been told that seeds are not the way to go if your data may vary a bit, since you'd have to delete all information and reinsert it again. Say.. I want to change or add categories, then I'd have to edit the seeds.rb file, do my modifications and then delete and reload all data...., is there another way? Or are seeds the defenitely best way to solve this problem?
So it sounds like you'll possibly be adding, changing, or deleting data along the way that will be intermingled amongst other data. So seeds.rb is out. What you need to use are migrations. That way you can search for and identify the data you want to change through a sequential process, which migrations are exactly designed for. Otherwise I think your best bet is to change the data manually through the rails console.
EDIT: A good example would be as follows.
You're using Capistrano to handle your deployment. You want to add a new Category, Toys, to your system. In a migration file then you would add Category.create(:name => "Toys") or something similar in your migration function (I forget what they call it now in Rails 3.1, I know there's only a single method though), run rake db:migrate locally, test your changes, commit them, then if it's acceptable deploy it using cap:deploy and that will run the new migration against your production database, insert the new category, and make it available for use in the deployed application.
That example aside, it really depends on your workflow. If you think that adding new data via migrations won't hose your application, then go for it. I will say that DHH (David Heinemeier Hansson) is not a fan of it, as he uses it strictly for changing the structure of the database over time. If you didn't know DHH is the creator of Rails.
EDIT 2:
A thought I just had, which would let you skip the notion of using migrations if you weren't comfortable with it. You could 100% rely on your db/seeds.rb file. When you think of "seeds.rb" you think of creating information, but this doesn't necessarily have to be the case. Rather than just blindly creating data, you can check to see if the pertinent data already exists, and if it does then modify and save it, but if it doesn't exist then just create a new record plain and simple.
db/seeds.rb
toys = Category.find_by_name("Toys")
if toys then
toys.name = "More Toys"
toys.save
else
Category.create(:name => "More Toys")
end
Run rake db:seeds and that code will run. You just need to consistently update the seeds.rb file every time you change your data, so that 1) it's searching for the right data value and 2) it's updating the correct attributes.
In the end there's no right or wrong way to do this, it's just whatever works for you and your workflow.
The place to load development data is db/seeds.rb. Since you can write arbitrary Ruby code there, you can even load your dev data from external files, for instance.
there is a file called db/seeds.rb
you can instantiate records using it
user1=User.create(:email=>"user#test.com",
:first_name=>"user",
:last_name=>"name",
:bio=>"User bio...",
:website=>"http://www.website.com",
:occupation=>"WebDeveloper",
:password=>"changeme",
:password_confirmation=>"changeme",
:avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
)
user2=User.create(:email=>"user2#test.com",
:first_name=>"user2",
:last_name=>"name2",
:bio=>"User2 bio...",
:website=>"http://www.website.com",
:occupation=>"WebDeveloper",
:password=>"changeme",
:password_confirmation=>"changeme",
:avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
)
Just run rake db:seed from command line to get it into the db

override mercurial username with username from apache authentication

I've set up a repository that is served through apache2. Users first need to authenticate to apache in order to read / write to the repository.
I has come to my attention, that if users set some crazy name as 'username', this name will used for the commit - and not the apache authentication name.
Now, is there a way so that either
the username is replaced by the apache login name?
or I add the apache login name to the username as defined in the commit?
I know that subversion & apache will always use the apache login name, so that should be possible with mercurial too, right?
EDIT:
I think what I need is to write a hook which extracts the http username and checks whether it matches the commit username. if it doesn't, then the push request should be rejected.
Does anyone know how to do this?
This is the wrong approach to this, and is guaranteed to cause more headache and problems than whatever problem it is that you're trying to solve right now.
Let's assume that you succeeded in implementing the proposed method, what would happen?
Well, in my local repository, that I'm trying to push, I have changesets 1, 2, and 3, with hashes ABC, DEF and KLM. For some reason, I did not use the apache username when committing, so they're wrong, according to your proposed changes.
I push to the server.
In-flight, your code changes my commits to have the apache username instead. This causes the hashes of those changesets to become recalculated, and different. In other words, my changeset 1, 2, and 3 will now have hashes XYZ, DEF and JKL.
So now my changes are on the server. I did not get a conflict during push since I was the last person cloning.
However, if I now try to pull, I now suddenly discover there are 3 changesets I don't have, so I pull those, and discover that I now have those 3 changesets in parallel with the 3 I had, with the same contents, a different committer name, and different hashes.
This is how every push and pull will behave from now on.
You push, and immediately you can pull the "same" changesets back, with new hashes, in a parallel branch to yours.
And now the fun begins. How does your local client discover how what to push? It asks the server, "what do you have?", and then compare that. Well, the server still doesn't have your 3 original changesets, so the outgoing-command is going to figure, well, those 3 changesets should be pushed.
But if you try to push that, you then recreate the same 3 new changesets, which can't be pushed, so you're going to have troubles with that.
What you have to do is impose the following workflow on your users:
Push the new changesets
Pull the new changesets back, in their new form
Strip out the original changesets that was pushed
A better approach would be for the server to prevent the push in the first place, with a message about using the wrong commit name.
Then you place the burden on the user to fix those changesets before trying to push, for instance by importing them into MQ and reapplying them one at a time.
Or... not.
What if I do a pull from you? You fix a bug, and you're not yet ready to push everything to the server, so you allow me to pull from you, and now I have outgoing changesets with your name on it, and a server that will enforce my name on them all.
About now you should realise that this approach is going to cause a lot of problems, you're basically trying to make a distributed version control system behave like a centralised version control system.

Drop and Recreate a single table (on Heroku)

My app is in beta, and I've been doing limited testing of a feature that involves a new model. After a fair amount of testing I had to make a structural change that makes the old data non-functional.
What I need to do is just drop and recreate one table. I know that I could do this in a migration, but that seems like such a hack. In a local dev copy I would just use db:reset, but in the beta app I don't want to lose data in any tables except this one.
Is this a simple way to instruct a production app to drop and recreate a single table. In my case, I'm deploying with Heroku, in case that affects how you would solve this issue.
To empty a table on Heroku without changing the schema, in your application's directory:
$ heroku run console
Ruby console for myap.heroku.com
>> ModelName.delete_all
>> exit
I know that I could do this in a migration, but that seems like such a hack.
It's not a hack. It's precisely what migrations are designed to do.
You need to rerun the migration for that table to make structural changes. I haven't used ActiveRecord before, but I'd also delete the data in the table using ModelName.delete_all from heroku console.
heroku run console
irb(main):001:0> ModelName.delete_all
And you are done.