Skip to content

Instantly share code, notes, and snippets.

View klochner's full-sized avatar

kevin lochner klochner

View GitHub Profile
Join Table Columns (2.3ms) SHOW FIELDS FROM `foos_bars`
Resident Load (18.7ms) SELECT * FROM `bars` INNER JOIN `foos_bars` ON `bars`.id = `foos_bars`.bar_id WHERE (`foos_bars`.foo_id = 1327 )
SQL (0.1ms) BEGIN
SQL (51.2ms) DELETE FROM `foos_bars` WHERE foo_id = 1327 AND bar_id in (117261,117262,117263, . .
@foo.connection.execute("delete from foos_bars where foo_id = #{@foo.id}")}
class Foo < ActiveRecord::Base
has_and_belongs_to_many :bars
end
class Bar < ActiveRecord::Base
has_and_belongs_to_many :foos
end
@foo = Foo.create
@bars = Bar.find_all_by_some_attribute(:a)
Join Table Columns (2.3ms) SHOW FIELDS FROM `foos_bars`
Bar Load (18.7ms) SELECT * FROM `bars` INNER JOIN `foos_bars` ON `bars`.id = `foos_bars`.bar_id WHERE (`foos_bars`.foo_id = 1327 )
SQL (0.1ms) BEGIN
SQL (51.2ms) DELETE FROM `foos_bars` WHERE foo_id = 1327 AND bar_id in (117261,117262,117263, . .
#try this data set:
ARR = [[1, "red"], [1, "green"],
[2, "red"],
[3, "yellow"],
[4, "red"], [4, "green"], [4, "yellow"],
[5, "green"], [5, "red"],
[6, "black"],
[7, "foo"],
[8, "bar"],
[9, "baz"],
FB.init({
appId : 'YOUR_APP_ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
def history(num=100)
h = Readline::HISTORY.to_a
start = [0,h.size-num-1].max
h.zip((0..h.size).to_a)[start...h.size].each do |e,i|
puts " #{(i).to_s.rjust(4)} #{e}"
end;nil
end
>> history(7)
483 def history(num=100)
#models/user.rb
def user_city
read_attribute(:user_city) || address && address.city || ""
end
#models/user.rb
class User < ActiveRecord::Base
has_one :address
named_scope :with_city_info,
:joins=>:address,
:select="users.*, addresses.city as user_city"
end
#models/address.rb
class Address < ActiveRecord::Base
#models/user.rb
named_scope :with_city_info,
:joins=>:address,
:select="users.*, addresses.city as user_city"
#controllers/users_controller.b
def locations
@users = User.with_city_info
end