Its very easy to add favicon to ur site by following steps
include the line in your layout head
html link tag
with attributes
rel="icon" href="favicon_path"
type="image/x-icon"
favicon_path = image path of favicon
That's it...
Try with this..
Monday, November 30, 2009
Friday, November 27, 2009
has_many_friends association
Hi Guys,
I have stuck while building the association for model user,
user has_many friends, friend - nothing but a user
For that i have created a model called 'Friendship', user to join user and friends. And build the association with the following relations
friendship table has the following fields
user_id
friend_id
class User < ActiveRecord::Base
has_many :friendships
has_many :friendships_by_me, :foreign_key => 'user_id', :class_name => 'Friendship'
has_many :friends_by_me, :through => :friendships_by_me, :source => :friendshipped_by_me
has_many :friendships_for_me, :foreign_key => 'friend_id', :class_name => 'Friendship'
has_many :friends_for_me, :through => :friendships_for_me, :source => :friendshipped_for_me
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friendshipped_for_me, :class_name => "User", :foreign_key => 'user_id'
belongs_to :friendshipped_by_me, :class_name => "User", :foreign_key => 'friend_id'
end
Thats it!!!
Now run the following commands
user = User.first
user.friends_by_me => [users] returns the collection of friends added by user.
user.friends_for_me => [users] returns the collection of friends who add user as their friend.
Note: i have referred the plugin 'has_many_friends'.
Thanks,
vaddi
I have stuck while building the association for model user,
user has_many friends, friend - nothing but a user
For that i have created a model called 'Friendship', user to join user and friends. And build the association with the following relations
friendship table has the following fields
user_id
friend_id
class User < ActiveRecord::Base
has_many :friendships
has_many :friendships_by_me, :foreign_key => 'user_id', :class_name => 'Friendship'
has_many :friends_by_me, :through => :friendships_by_me, :source => :friendshipped_by_me
has_many :friendships_for_me, :foreign_key => 'friend_id', :class_name => 'Friendship'
has_many :friends_for_me, :through => :friendships_for_me, :source => :friendshipped_for_me
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friendshipped_for_me, :class_name => "User", :foreign_key => 'user_id'
belongs_to :friendshipped_by_me, :class_name => "User", :foreign_key => 'friend_id'
end
Thats it!!!
Now run the following commands
user = User.first
user.friends_by_me => [users] returns the collection of friends added by user.
user.friends_for_me => [users] returns the collection of friends who add user as their friend.
Note: i have referred the plugin 'has_many_friends'.
Thanks,
vaddi
Sunday, November 15, 2009
Drop down methods
java script methods for drop down.
selectedIndex - to get the selected index value
options[].value - to get the value of the selected item
options[].text - to get the option for the selected item
ex:
var list_box = document.getElementById('id_of_dropdown_box');
list_box.selectedIndex; --> returns the value of selected index(drop down index starts from 0) '1' check
options[0].value --> value of the zero th element '1' check
options[0].text --> text of the zero th element 'Ruby' check
selectedIndex - to get the selected index value
options[].value - to get the value of the selected item
options[].text - to get the option for the selected item
ex:
var list_box = document.getElementById('id_of_dropdown_box');
list_box.selectedIndex; --> returns the value of selected index(drop down index starts from 0) '1' check
options[0].value --> value of the zero th element '1' check
options[0].text --> text of the zero th element 'Ruby' check
ruby - 'yield' method
Hi Guys,
Hope most of our ROR developers are using 'yield' method in the layout file. In ruby we are not using the yield method(called from any method). I have come across the 'yield' method, its very interesting to know about the use of that method.
We can use 'yield' method inside any ruby method to execute the code inside the block.
example-1:
class Sample
def check_yield
yield
yield
yield
end
end
in the class 'Sample', i have called the yield method three times inside the method.
obj = Sample.new()
obj.check_yield{puts "hi am in method block"}
Output:
hi am in method block
hi am in method block
hi am in method block
Now i have created an object for the class 'Sample', and called the method 'check_yield' with the block({ }). When i executed the method it prints the results for three times. Because we are calling the yield for three times, the call to yield method executes the code inside the block({puts "hi am in method block"}). That's it.
We can also use the yield method with params.
example-2:
class Sample
def even_nos_upto(param)
for i in 0..param
yield(i) if i%2 == 0
end
end
end
obj = Sample.new()
obj.even_nos_upto(10){|x| puts x}
Output:
0
2
4
6
8
10
In Array, we are using the method 'collect', 'each' to get each value of the array.
[1,2,3,4,5].collect{|x| puts x}
Inside the collect method in Array class, the yield method called to return each value.
class Array
def my_collect
for i in 0..(self.length-1)
value = self[i]
yield(value)
end
end
def collect_greater_than(limit)
for i in 0..(self.length-1)
value = self[i]
yield(value) if value > 5
end
end
end
[1,2,3,4,5,6,7].collect{|x| puts x} # default method collect
[1,2,3,4,5,6,7].my_collect{|x| puts x} # method my_collect
[1,2,3,4,5,6,7].collect_greater_than(5){|x| puts x} # collect with condition
Output:
1
2
3
4
5
6
7
1
2
3
4
5
6
7
6
7
In the above sample, the method 'collect' & 'my_collect' works like a same. And the method collect_greater_than bit different from the first one, it returns the values based on the condition we have used.
Hope most of our ROR developers are using 'yield' method in the layout file. In ruby we are not using the yield method(called from any method). I have come across the 'yield' method, its very interesting to know about the use of that method.
We can use 'yield' method inside any ruby method to execute the code inside the block.
example-1:
class Sample
def check_yield
yield
yield
yield
end
end
in the class 'Sample', i have called the yield method three times inside the method.
obj = Sample.new()
obj.check_yield{puts "hi am in method block"}
Output:
hi am in method block
hi am in method block
hi am in method block
Now i have created an object for the class 'Sample', and called the method 'check_yield' with the block({ }). When i executed the method it prints the results for three times. Because we are calling the yield for three times, the call to yield method executes the code inside the block({puts "hi am in method block"}). That's it.
We can also use the yield method with params.
example-2:
class Sample
def even_nos_upto(param)
for i in 0..param
yield(i) if i%2 == 0
end
end
end
obj = Sample.new()
obj.even_nos_upto(10){|x| puts x}
Output:
0
2
4
6
8
10
In Array, we are using the method 'collect', 'each' to get each value of the array.
[1,2,3,4,5].collect{|x| puts x}
Inside the collect method in Array class, the yield method called to return each value.
class Array
def my_collect
for i in 0..(self.length-1)
value = self[i]
yield(value)
end
end
def collect_greater_than(limit)
for i in 0..(self.length-1)
value = self[i]
yield(value) if value > 5
end
end
end
[1,2,3,4,5,6,7].collect{|x| puts x} # default method collect
[1,2,3,4,5,6,7].my_collect{|x| puts x} # method my_collect
[1,2,3,4,5,6,7].collect_greater_than(5){|x| puts x} # collect with condition
Output:
1
2
3
4
5
6
7
1
2
3
4
5
6
7
6
7
In the above sample, the method 'collect' & 'my_collect' works like a same. And the method collect_greater_than bit different from the first one, it returns the values based on the condition we have used.
Subscribe to:
Posts (Atom)