background preloader

Rails

Facebook Twitter

Sphinx

Activerecord. Installing Rails on CentOS 5 « catapult-creative.com. Run Fat Boy .net » Blog Archive » Rails Migrations - Command Lin. I absolutely loves Rails 2.0 and the command line conveniences it provides for the RESTful scaffolding and models. Here’s a couple of tricks to generate a migration to quickly to add/remove columns from an existing table. Assume you already have a table called ‘Users’. You would like to add a column called ‘title’ and another column called ‘email’. You can quickly create a migration from the command line using the following : script/generate migration AddTitleEmailToUsers title:string email:string The migration generated looks like this: class AddTitleEmailToUsers < ActiveRecord::Migration def self.up add_column :users, :title, :string add_column :users, :email, :string end def self.down remove_column :users, :email remove_column :users, :title end end And if you need to remove those same columns, you can generate a migration with the following : script/generate migration RemoveTitleEmailFromUsers title:string email:string The title of the migration is significant.

How sweet is that?!? Checkbox list in Ruby on Rails using HABTM | Nobody Listens Anyw. Checkboxes are one of those things that look easy and should be easy, but they aren't always easy. I needed a solution that could create a checkbox list of languages that a user speaks. So I don't forget here's how to do it: The migrations are important. You have to be sure to exclude the id parameter when you create languages_users or you will get ' Mysql::Error: #23000Duplicate entry' due to the fact that ActiveRecord will try to store a value in the id field that indicates which model created the entry (User.languages << vs Langauges.users).

The other option is the create the id parameter so that the direction is maintained but be sure that it is not created as a primary key. class LanguagesUsers < ActiveRecord::Migration def self.up create_table :languages_users, :id => false, :force => true do |t| t.integer :user_id t.integer :language_id t.timestamps end end def self.down drop_table :languages_users endend Here are my models: user.rb language.rb: On to the view: should be this: Rails::Full text search in Ruby on Rails 2 - MySQL. My previous post compared MySQL and ferret full text search engines. For our project, the ferret was the winner. Nevertheless, I will try to show the beauty and simplicity of using MySQL indexes. Create table and indices First of all it is necessary to create table and the corresponding index. CREATE TABLE articles( id integer NOT NULL PRIMARY KEY AUTO_INCREMENT, title varchar(20), body varchar(100), fulltext(title, body)) engine = MyISAM;<BR> or create index after the table exists.

CREATE fulltext INDEX x_f_articles_body ON articles(body); Please note the MyISAM engine. And now, let’s insert some data INSERT INTO articles(title, body) SELECT "Databases and IT", "Todays world ... database... Query syntax Querying is simple. SELECT * FROM articles WHERE match(title,body) against ("Databases"); or you can create query looking for all database related articles SELECT * FROM articles WHERE match(title,body) against ("Databases" WITH query expansion); See also the record ID=5. This is useful. Search. Rails:db:migrate:data-autopopulation. A Guide to The Rails Command Line. 1 Command Line Basics There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are: rails consolerails serverbin/railsrails generaterails dbconsolerails new app_name All commands can run with -h or --help to list more information. Let's create a simple Rails application to step through each of these commands in context. 1.1 rails new The first thing we'll want to do is create a new Rails application by running the rails new command after installing Rails.

You can install the rails gem by typing gem install rails, if you don't have it already. Rails will set you up with what seems like a huge amount of stuff for such a tiny command! 1.2 rails server The rails server command launches a web server named Puma which comes bundled with Rails. With no further work, rails server will run our new shiny Rails app: With just three commands we whipped up a Rails server listening on port 3000. 1.3 rails generate 1.4 rails console. Alex Young: Rapid Rails Part 1: Command Line Mastery. Rapid Rails is a series of articles containing succinct tips to increase your productivity when working with Ruby on Rails. This is the first part, and shows you how to make the most of the command–line tools that come with Rails. Quickly generate migrations The Rails generator script (found inside an application’s directory at script/generate) allows you to quickly create templates for anything you need within Rails. As well as models and controllers, you can also use it to write entire migrations: script/generate migration AddIPAddressToUsers ip_address:string The field definition is specified as field_name:type, and you can type in as many as you need for each migration.

Script/generate migration This cuts down the hassle of changing database schemas around, and should be the nail in the coffin for lazy database schema manipulation with GUI tools. Get more generators What was I doing last week? Rake notes rake notes:fixme rake notes:optimize rake notes:todo More Rake house–keeping tools Or: Ruby on Rails - Rails Migrations Cheatsheet - Dizzy. How to Setup a Rails application in cPanel « Blagador! – marvspa.

Ruby on Rails 101. ActionView is the module in the ActionPack library that deals with rendering a response to the client. The controller decides which template and/or partial and layout to use in the response Templates use helper methods to format text, generate links, forms, and JavaScript. Templates that belong to a certain controller typically live under app/view/controller_name, i.e. templates for Admin::UsersController live under app/views/admin/users Templates shared across controllers are put under app/views/shared. You can render them with render :template => ‘shared/my_template’ You can have templates shared across Rails applications and render them with render :file => ‘path/to/template’ Templates have access to the controller objects flash, headers, logger, params, request, response, and session.

Instance variables (i.e. @variable) in the controller are available in templates The current controller is available as the attribute controller. atom_feed do |feed| feed.title("My great blog! ") # 1. Ruby on Rails « Gregory Doran. While working on a CRM system in Ruby on Rails 2.2 I needed to find a way to email one of the users a weekly report.

The report would be generated from the PDF templates already integrated with the front-end using the RTex plug-in for Rails. The solution was to include the following in an ActionMailer model to render the view away from the controller and attach the result to an email. Firstly, create an instance of the ActionView::Base class: 1.av = ActionView::Base.new(Rails::Configuration.new.view_path) The next step is to call render which will return the output of the template. 2.latex = av.render(:file => "app/views/people/index.pdf.rtex", 3. 4. The arguments specify the layout to use and the local variables that are to be passed to the view. If you're using partials you need to specify explicitly the file extension of the partial (eg. 5.pdf = RTeX::Document.new(latex).to_pdf. Adding a method to built-in class in rails app.

Rails Framework Documentation. Open Flash Chart | Ruby on Rails India, Ruby on Rails in India, A Guide to Active Record Associations. 1 Why Associations? Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this: Now, suppose we wanted to add a new order for an existing customer.

Or consider deleting a customer, and ensuring that all of its orders get deleted as well: With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. With this change, creating a new order for a particular customer is easier: Deleting a customer and all of its orders is much easier: To learn more about the different types of associations, read the next section of this guide. 2 The Types of Associations In Rails, an association is a connection between two Active Record models.

Projects: Open Flash Chart II Plugin for Ruby on Rails - Graphs. Heroku Garden.