Posts Tagged ‘active record’

Rails Plugin Acts as Taggable on Steriods

Posted 02 Mar 2010 — by admin
Category Ruby on Rails

You can download it here http://github.com/suitmymind/acts-as-taggable-on-steroids as well as read usage info (which is for the most part reprinted here).

./script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids
./script/generate acts_as_taggable_migration
rake db:migrate

Then in your model

class Post < ActiveRecord::Base
    acts_as_taggable
 end

And usage is as follows

p = Post.find(:first)
p.tag_list # []
p.tag_list = "Funny, Silly"
p.save
p.tag_list # ["Funny", "Silly"]
p.tag_list.add("Great", "Awful")
p.tag_list.remove("Funny")

#to find...
Post.find_tagged_with('Funny, Silly')
Post.find_tagged_with('Funny, Silly', :match_all => true)

To use this in a form and let users enter a comma separated list of tag names...

form_for @post do |f|
f.text_field :tag_list

And to get a tag cloud

  #controller
  class PostController < ApplicationController
    def tag_cloud
      @tags = Post.tag_counts
    end
  end

  # and in view...
  <style>
  .css1 { font-size: 1.0em; }
  .css2 { font-size: 1.2em; }
  .css3 { font-size: 1.4em; }
  .css4 { font-size: 1.6em; }
  </style>

  <% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %>
    <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
  <% end %>

*Note. If you have a controller "tags_controller.rb" and the auto generated (if you used ./script/generate) helper file "tags_helper.rb" you'll need to make sure to copy the contents of the plugin lib module of the same name, into the helper file. You'll get an error otherwise.

Descending Sort By in Model For Active Record Hash on Created_at attribute

Posted 10 Nov 2009 — by admin
Category Ruby on Rails

If you have a couple collections from the database and you want to sort it without the help of Active Record, take a look at the sort_by method on Array type. I’ve used this before when I have a couple of collections which are slightly different but I need them in a chronological order.


  @posts_group_a = Post.find :all, :conditions => ["user_id = ?", current_user.id]
  @posts_group_b = Post.find :all, :conditions => ["user_id = ?", friend_user.id]

  #merge the two arrays here
  @posts = @posts_group_a + @posts_group_b

  # notice the "-" is for descending order and the "to_i" casts the date time to an integer (required)
  @posts.sort_by {|post| - post.created_at.to_i}

Output Logger and SQL to the Rails Console in Development Mode

Posted 10 Nov 2009 — by admin
Category Ruby on Rails

If you want to take a look at the SQL being generated by active record while your using the console, you can either type this into the console when it loads

ActiveRecord::Base.logger = Logger.new(STDOUT)

Or you can add it to your environment so that it’ll be the default behavior
rails_root/config/environments/development.rb

#...
ActiveRecord::Base.logger = Logger.new(STDOUT)

It’s a nice way to keep you away of any expensive queries you may unknowingly be writing!

Acts_as_versioned Rails Plugin

Posted 17 Sep 2009 — by admin
Category Ruby on Rails

Versioning models with the acts_as_versioned plugin

cd rails/app
./script/plugin install git://github.com/technoweenie/acts_as_versioned.git
./script/generate model post title:string body:text

In your model

class Post < ActiveRecord::Base
  acts_as_versioned
end

In db/migrate/****_create_posts.rb

  def self.up
    create_table :posts do |t|
      t.string :title
      t.text :body
      t.timestamps
    end
    Post.create_versioned_table
  end

  def self.down
    drop_table :posts
    Post.drop_versioned_table
  end

Migrate your db

rake db:migrate

Usage

p = Post.create :body => "hello world"
p.body = "HELLO WORLD"
p.save

p.versions.size
p.versions.last
p.revert_to(p.versions.first)
p.body # => hello world

*Quick Note If you want to revert to an older version in a controller or something, don't do

@post = @post.revert_to(2)

Revert_to method will return a TrueClass, Boolean type. Instead use

@post.revert_to(2)

This method will update the attributes for you and when you call them you'll get that version.

More information is available here http://ar-versioned.rubyforge.org/ and http://www.urbanhonking.com/ideasfordozens/archives/2006/02/learns_to_use_a_1.html