I need to maintain fixed number of records. While my method is going to insert a new record then the old record will be delete automatically if the number of records reached to max length.
Max_length = 4
def create
@students = Student.new(params[:student])
if @students.save
if Student.find(:all).length >= Max_length
@old_students = Student.first
@old_students.destroy
end
end
end
Friday, May 21, 2010
Thursday, May 20, 2010
Disable right click using javascript
<script>
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>
Saturday, May 15, 2010
Specifying Document Compatibility Modes
To specify a document mode for your Web pages, use the meta element to include an X-UA-Compatible http-equiv header in your Web page. The following example specifies Emulate IE7 mode compatibility.
<html>
<head>
<!-- Mimic Internet Explorer 7 -->
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" >
<title>My Web Page</title>
</head>
<body>
<p>Content goes here.</p>
</body>
</html>
This example tells Internet Explorer 8 to display a Web page the same way that Internet Explorer 7 displayed it.
<html>
<head>
<!-- Mimic Internet Explorer 7 -->
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" >
<title>My Web Page</title>
</head>
<body>
<p>Content goes here.</p>
</body>
</html>
This example tells Internet Explorer 8 to display a Web page the same way that Internet Explorer 7 displayed it.
Friday, May 7, 2010
Delegation with Rails
Why delegation?
Before going to detail let me start with an example , we have a two models named User and profile having has-one association.
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
Suppose Profile have attributes like first_name, last_name, phone etc and @user is the instance of User class.now to find name or phone of an user we have two ways -
1. Using dot-magic
FirstName = @user.profile.first_name
This is simple but becomes bulky for long chain associated object.For example @user.sth.sth.sth.sth…some_attribute
2. Using delegation
Instead we can delegate desired attributes in User model. Here is the code to add
class User < ActiveRecord::Base
has_one :profile
[:first_name, :last_name, :phone, ...etc.].each {|attr| delegate attr, :to => :profile}
end
Lets see usage of delegation in depth
Delegation in Rails
In ruby on rails Delegation provide us a fine way over dot-magic. Using delegation it is easy to access associated object’s attributes. Delegation is a feature Rails introduced in it’s 2.2 version.The concept of delegation is to take some methods and send them off to another object to be processed.Delegate simply delegates a method to another class.This is useful if you have a lot of cases where you are referring to fields in associated classes.
Let me explain this with a brief example:
Suppose you have a User class for anyone registered on your site, and a Consumer class for those who have actually placed orders:
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
end
As for now, if you are in a Consumer instance, you can get their User information doing @consumer.user.name, or @consumer.user.email. Delegation allows you to simplify this:
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:email,:to => :user
end
Now you can refer to @consumer.name and @consumer.email to retrieve and set values for those attributes directly.
This is all well and good if you can be sure that there will always be a user for any given consumer. If consumer’s user is nil and name is called, however, an exception will be raised (because nil.name is undefined). There’s a new feature in Rails 2.2 that relate to delegation and using a prefix for delegated methods also you can set default value by extending delegate method
Delegate with default
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:to => :user, :default => "Sapna Prajapati"
end
Delegate with prefixes
Delegate prefixes just appeared in edge Rails and will work in Rails 2.2. If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:email,:to => :user ,prefix => true
end
This will produce delegated methods @consumer.user_name and @consumer.user_email.
The problem is if you refactor user or consumer , you’ll have to go back and fix all the places you did this access chaining as well. Means if you rename the user class, you’re almost always going to want to rename that prefix too.But delegates provide custom prefixes so you will end up to change.
Delegate with custom prefixes
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:email,:to => :user ,prefix => :owner
end
This will produce delegated methods @consumer.owner_name and consumer.owner_email.
Before going to detail let me start with an example , we have a two models named User and profile having has-one association.
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
Suppose Profile have attributes like first_name, last_name, phone etc and @user is the instance of User class.now to find name or phone of an user we have two ways -
1. Using dot-magic
FirstName = @user.profile.first_name
This is simple but becomes bulky for long chain associated object.For example @user.sth.sth.sth.sth…some_attribute
2. Using delegation
Instead we can delegate desired attributes in User model. Here is the code to add
class User < ActiveRecord::Base
has_one :profile
[:first_name, :last_name, :phone, ...etc.].each {|attr| delegate attr, :to => :profile}
end
Lets see usage of delegation in depth
Delegation in Rails
In ruby on rails Delegation provide us a fine way over dot-magic. Using delegation it is easy to access associated object’s attributes. Delegation is a feature Rails introduced in it’s 2.2 version.The concept of delegation is to take some methods and send them off to another object to be processed.Delegate simply delegates a method to another class.This is useful if you have a lot of cases where you are referring to fields in associated classes.
Let me explain this with a brief example:
Suppose you have a User class for anyone registered on your site, and a Consumer class for those who have actually placed orders:
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
end
As for now, if you are in a Consumer instance, you can get their User information doing @consumer.user.name, or @consumer.user.email. Delegation allows you to simplify this:
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:email,:to => :user
end
Now you can refer to @consumer.name and @consumer.email to retrieve and set values for those attributes directly.
This is all well and good if you can be sure that there will always be a user for any given consumer. If consumer’s user is nil and name is called, however, an exception will be raised (because nil.name is undefined). There’s a new feature in Rails 2.2 that relate to delegation and using a prefix for delegated methods also you can set default value by extending delegate method
Delegate with default
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:to => :user, :default => "Sapna Prajapati"
end
Delegate with prefixes
Delegate prefixes just appeared in edge Rails and will work in Rails 2.2. If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:email,:to => :user ,prefix => true
end
This will produce delegated methods @consumer.user_name and @consumer.user_email.
The problem is if you refactor user or consumer , you’ll have to go back and fix all the places you did this access chaining as well. Means if you rename the user class, you’re almost always going to want to rename that prefix too.But delegates provide custom prefixes so you will end up to change.
Delegate with custom prefixes
class User < ActiveRecord::Base
belongs_to :consumer
end
class Consumer < ActiveRecord::Base
has_one :user
delegate :name,:email,:to => :user ,prefix => :owner
end
This will produce delegated methods @consumer.owner_name and consumer.owner_email.
Subscribe to:
Posts (Atom)