Archive

Posts Tagged ‘ror’

How To Start A Rails Edge App The Easy Way

January 4th, 2009

There’s a lot of cool stuff pooring in about what’s new in Rails Edge (which will become Rails 2.3 and/or Rails 3).

Most likely you can’t wait to get started with these new features, especially when you’re about to start a new project, which doesn’t have to be stable yet, but will be by the time 2.3/3.0 come out. This post shows you the way to create a new Rails app based on the most current Rails code, also called Edge Rails.

Let’s go…

mkdir -p myapp/vendor
cd myapp
git init
git submodule add git://github.com/rails/rails.git vendor/rails
git commit -m "Frozen Rails Edge as submodule"
ruby vendor/rails/railties/bin/rails .
# Add generated files to git, and code on...

First, you create a new directory for your app, including the vendor directory. Easy, right?

Next, you initialize a Git repository for your empty project. We’ll be using Git to track the remote Rails Edge code. Stay with me.

By adding a Git submodule we tell git to clone the code from git://github.com/rails/rails.git into the vendor/rails directory. Nice! If you check the current git status with git status you see git has already staged two files for you, .gitmodules and vendor/rails. Commit them now to attach the submodule to your local git repository.

Git will not automatically update your submodule, you’ll have to do that by hand. I’ll show you this in a minute.

With vendor/rails containing Rails Edge, you can now generate your Rails Edge application. In you project directory (myapp/), you call ruby vendor/rails/railties/bin/rails .. This will generate a new Rails Edge application in the current directory.

Now it’s up to you to create a fitting .gitignore file and commit the files to your repository.

That’s all, you now have a new Rails Edge application. Try ruby script/server to see it all in action. Enjoy!

Cloning your project

At some point you’ll push your myapp project to a remote git server. When you clone a fresh copy, you’ll have to initialize the git submodules. This is quite easy:

git submodule init
git submodule update

Updating Rails Edge

As I said earlier, Git will not keep your submodules up-to-date for you, but will stick with the revision you added. To keep track of Rails Edge’s progress, you’ll need to update the submodule. This is done like this:

cd myapp/vendor/rails
git remote update
git merge origin/master

This will update your Rails Edge code. Make a commit, stating you updated the code!

After updating Rails Edge, you may want to update your rails application (like javascript files, config files etc).

rake rails:update

Good luck! And happy coding!

General , , , , ,

ActiveRecord Read Only Model

August 17th, 2008

ActiveRecord is great in providing CRUD for your data models. In some cases, however, it’s necessary to prevent write access to these models. The data may be provided by an external source and should only be used as a reference in your application, for example.

I’m going to show you how you can easily mark a Model as read only all the time. In this example I have a Item model like this:

class Item < ActiveRecord::Base
end

ActiveRecord::Base provides two methods that may be of interest here:

def readonly!
  @readonly = true
end
 
def readonly?
  defined?(@readonly) && @readonly == true
end

The first method sets the record to read only. This is great, but we don’t want to set the read only property every time we load a model. The second, readonly?, return true if the object is read only or false if it isn’t.

So, if we return true on the readonly? method, our object is marked as read only. Great!

class Item < ActiveRecord::Base
  def readonly?
    true
  end
end

That is all! All Item objects are now marked as read only all the time. If you try to write to the model, you’ll receive an error.

item = Item.find(:first)
item.update_attributes(:name => 'Some item name')
=> ActiveRecord::RecordReadOnly

Blog, General , , ,