I want to write a named scope to get a record from its id.
For example, I have a model called Event, and I want to simulate Event.find(id) with use of named_scope for future flexibility.
I used this code in my model:
named_scope :from_id, lambda { |id| {:conditions => ['id= ?', id] } }
and I call it from my controller like Event.fr...
In my rails app I have a need to save some webpages and display them to the user as images. For example, how would I save www.google.com as an image?
...
I've written a scrubyt extractor based on the 'learning' technique - that is, specifying the current text on the page and getting it to work out the XPath expressions itself. However, I now want to export the extractor so that it can be used even when the page has changed.
The documentation for scrubyt seems to be all over the place now...
Hi there!
I'm working my way through adapting a template I have been given that is basically a list of products for sale. I want to change it from a top-down list into a table layout.
I want to end up with something as follows-
<div id= 'ladiesproducts'>
<% ladies_products = hosting_products.find_all do
|product|
product.name.match("lad...
Consider this very simple logging class:
class MockLog
# ...
end
Let's add logging to all our classes:
class Module
def has_logging()
class_eval {
@log = MockLog.new
def log
self.class.instance_variable_get :@log
end
}
end
end
Now, why doesn't this work?
class Foo
has_logging
end
Foo.new.l...
I'm trying to write a DSL that allows me to do
Policy.name do
author "Foo"
reviewed_by "Bar"
end
The following code can almost process it:
class Policy
include Singleton
def self.method_missing(name,&block)
puts name
puts "#{yield}"
end
def self.author(name)
puts name
end
def self.reviewed_by(name)
put...
i've inherited a site that in production is generating dozens of "no block given" exceptions every 5 minutes.
the top of the stack trace is:
vendor/gems/nkallen-cache-money-0.2.5/lib/cash/accessor.rb:42:in `add'
vendor/gems/nkallen-cache-money-0.2.5/lib/cash/accessor.rb:33:in `get'
vendor/gems/nkallen-cache-money-0.2.5/lib/cas...
Sometimes I like to pop open the console script that comes with Rails to test small excerpts of code. That code normally involves some more involved ActiveRecord queries. Although not an expert in ActiveRecord, I'm proficient with SQL and want to see what it's translating underneath the hood for efficiency purposes. This will help me ref...
I love using HAML for HTML documents. It has clean syntax that's much more attractive than ERB. It works perfectly for HTML documents.
What about for non-HTML? Such as, for example, an email or text document with certain automatically-substituted components? I've been falling back to ERB, but don't like the heavy syntax compared to HAML...
I'm using MacPorts in order to manage my Ruby/Rails/Gems installations. Recently after doing a gem install wirble, wirble fails to load when I start an instance of irb. Here's the output:
$ irb --simple-prompt
Couldn't load Wirble: no such file to load -- wirble
The Wirble gem doesn't show up in my $LOAD_PATH:
>> puts $:
/opt/local/l...
I'm writing a Ruby class that extends TCPSocket. Assume it looks something like this:
class FooSocket < TCPSocket
def hello
puts 'hello'
end
end
I have a TCPServer listening for incoming connections
server = TCPServer.new 1234
socket = server.accept
When my server finally accepts a connection, it will return a TCPSo...
I have an ActiveRecord model object Foo; it represents a standard database row.
I want to be able to display modified versions of instances of this object. I'd like to reuse the class itself, as it already has all the hooks & aspects I'll need. (For example: I already have a view that displays the appropriate attributes). Basically I wa...
I have a model Foo that has_many 'Bar'. I have a factory_girl factory for each of these objects. The factory for Bar has an association to Foo; it will instantiate a Foo when it creates the Bar.
I'd like a Factory that creates a Foo that contains a Bar. Ideally this Bar would be created through the :bar factory, and respect the build st...
I'm trying out to have a block in a while and begin statements in Ruby, but I get a syntax error. Any other way to implement it?
Here's what I want to accomplish
(1..limit).each { |i|
while (true) do |n|
x = n * (i%n)
puts n if n%i != 0
break if x.even? && !x.zero?
n += 1
end
}
...
I have an ActiveRecord model class Foo that has_many Bar. I want to clone a Foo (to get duplicates of most of its attributes) and then modify its Bar instances.
This is a problem because cloned ActiveRecord instances share the same associated array; changes to one affect the other.
f1 = Foo.new
b = Bar.new
f1.bars << b
f2 = f1.clone
f2...
Having a bit of difficulty figuring out how to create a named_scope from this SQL query:
select * from foo where id NOT IN (select foo_id from bar) AND foo.category = ? ORDER BY RAND() LIMIT 1;
Category should be variable to change.
What's the most efficient way the named_scope can be written for the problem above?
...
I've been programming in Ruby for a while now with just the standard MRI implementation of Ruby, but I've always been curious about the other implementations I hear so much about.
I was reading about Rubinius the other day, a Ruby interpreter written in Ruby. I tried looking it up in various places, but I was having a hard time figurin...
Ciao a tutti,
nella Rails app che sto realizzando, l'utente ha la possibilita' di
inserire un post (campo memo) con all'interno una URL.
Ad esempio:
"bla bla bla bla www.blabla.com bla bla bla ..."
Nel mostrare tale post vorrei che www.blabla.com diventasse un link
cliccabile (come avviene in twitter).
Ovviamente deve essere fatto un...
I've been a Ruby developer for the past 4-5 years, and prior to that coded in Perl and a language called ProvideX for years.
As hard as it may seem, I've never written a Java application short of the basic Hello World app probably a decade ago.
I'm beginning to start doing some Android development to port some iPhone applications we di...
Just about to extend the Array class with the following extension:
class Array
def shuffle!
size.downto(1) { |n| push delete_at(rand(n)) }
self
end
end
However, I was wondering where a good place to keep these sort of extensions. I was thinking environment.rb or putting in its own file in the initializers directory.
...