Wednesday 28 October 2015

how to access controller method as helper_method?


Declare a controller method as a helper. For example, the following makes the current_user controller method available to the view:

class ApplicationController < ActionController::Base
helper_method :current_user

def current_user
@current_user ||= User.find_by_id(session[:user])
end
end

In a view:

Welcome, <%= current_user.name %>

Wednesday 21 October 2015

creating instance based on model name :



var_name = Admin::User.model_name.param_key

# var_name is admin_user

to make admin_user as instance variable:

instance_variable_set("@#{var_name}", Admin::User.last)

now,

@admin_user give you a last record of Admin::User model.

wow...........

Friday 16 October 2015

Use model association in create:


Use model association in create:

class BlogsController < ApplicationController
def create
@blog = Blog.new(params[:blog])
@blog.user_id = current_user.id
@blog.save
end
end

In this example, user_id is assigned to @blog explicitly. It's not too big problem, but we can save this line by using model association.

class BlogsController < ApplicationController
def create
@blog = current_user.blogs.build(params[:blog])
@blog.save
end
end

class User < ActiveRecord::Base
has_many :blogs
end

We define the association that user has many blogs, then we can just use current_user.blogs.build or current_user.blogs.create to generate a blog, and the current_user's id is assigned to the user_id of the blog automatically by activerecord.

Select specific fields for performance:

Select specific fields for performance:

database schema fo user model:

class User.rb
#fields
field :email, type: String, default: ""
field :encrypted_password, type: String, default: ""
field :first_name, type: String
field :last_name, type: String
field :age, type: Integer
field :sign_in_count, type: Integer, default: 0
field :current_sign_in_at, type: Time
field :last_sign_in_at, type: Time
field :current_sign_in_ip, type: String
field :last_sign_in_ip, type: String
field :provider, type: String
field :uid, type: String
field :description, type: String
end

The users index page displays 10 posts. At the beginning, I used

class UsersController < ApplicationController
  def index
    @posts = User.paginate(:page => params[:page])
  end
end

sql query:

SELECT * FROM users  LIMIT 0, 10

It was very slow when the user has huge description.

In rails there is a method "select" to fetch specific fields, Here I give you the way.

class User
  INDEX_COLUMNS = column_names - ['description', 'encrypted_password', sign_in_count', 'current_sign_in_at', 'last_sign_in_at', 'current_sign_in_ip', 'last_sign_in_ip']
end

class PostsController < ApplicationController
  def index
    @posts = Post.select(Post::INDEX_COLUMNS).paginate(:page => params[:page])
  end
end

Here rails tells database fetch only name, email, age, uid

SELECT name, email, age, uid, description FROM `posts` LIMIT 0, 10

It is faster than before and use less memory.

NOTE:
You should not select specific fields if you use memory object caching system, such as memcache.

Wednesday 14 October 2015

write a helper method to generate link_to with in li


application_helper.rb

def link url_path, class_name, title
content_tag :li do
link_to title, url_path, title: title, class: 'class_name'
end
end

usage:

in app.html.erb file:

<ul>
<%= link users_path, 'user', 'user' %>
</ul>

that's it....

Convert BSON::ObjectId to string and string to BSON::ObjectId


Convert BSON::ObjectId to string:
1.9.3-p125 :080 > profile = Profile.last.id
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)
1.9.3-p125 :083 > profile_id = profile.to_s
=> “4fe969dd79216d0af9000002”

Convert string to BSON::ObjectId:
1.9.3-p125 :084 > BSON::ObjectId.from_string(profile_id)
 => BSON::ObjectId(‘4fe969dd79216d0af9000002’)

Wednesday 7 October 2015

Use of scope with validates_uniqueness_of rails

There is a :scope option that you can use to specify other attributes that are used to limit the uniqueness check:

Eg: I want to validate name should be unique with in the section

validates_uniqueness_of :name, scope: :section_id