For Example, I want to know the User have many posts. So, I can get back post using this :
@user.posts
but I don't want to get all the posts back. I would like to limite the result, for example, top ten created, or may be sorted by some column. How can I do so? Thank you.
...
module Hints
module Designer
def self.message
"Hello, World!"
end
end
end
is there any way to use the following code to access the message method?
p Hints.Designer.message
Instead of
p Hints::Designer.message
...
(Big edit, I got part of the way there…)
I've been hacking away and I've come up with this as a way to specify things that need to be done before attributes are read:
class Class
def attr_reader(*params)
if block_given?
params.each do |sym|
define_method(sym) do
yield
self.instance_variable_get("@...
I want to add the option to register with a JSON format and some other specific stuff to my application. I've tried adding my own RegistrationsController controller and then setting my routes to look like this:
devise_for :users, :path_names => { :sign_in => 'signin', :sign_out => 'signout', :sign_up => 'signup' }
match 'signin', :to =>...
Using cap deploy:setup the deprec recipe.rb for mongrel_cluster runs as expected. This is fine (and indeed desired) for my production enviroment, but is it possible to cause mongrel_cluster/recipes.rb to NOT run when when I set a specific envrionment, e.g.
if ENV["STAGE"] == "production"
# service as normal
elsif ENV["STAGE"] == "st...
ok, easier to read version of the code (I hope)
class First
attr_accessor :addresses
def initialize
address
end
def address
@addresses= []
File.open("/RubyDev/useful/lib/list/listtest.txt").each_line do |i|
@addresses << i.chomp
end
end
end
class Server1
b = Last.new
pu...
Hi,
I need to write a Ruby script that can take a collection of jpeg files and creat a slideshow of them in .flv format. the .flv file then needs to be stored into a database.
Does anyone know what libraries/gems/I'm going to need to get this started?
...
I am fighting with lock timeout problem on mysql right now. The front-end code is in Ruby on Rails, which updates some tables. In mysql log, I see things like the following
Transaction 1 ... process no 3353, OS thread id 1094527296 inserting
INSERT INTO ... (caused by code in Ruby on Rails)
record lock for index 'PRIMARY' table 'A...
When developing & debugging, I sometimes wish I could write a 1-liner that dumped the names, types & values of a bunch of variables. The problem is I don't know how to access the name of a variable, if I can at all.
Here is a first attempt:
foo = 1
bar = "42"
baz = Hash.new
[foo, bar, baz].each do |v|
puts "#{v.???} = (#{v.class}...
Suppose I have a class in Ruby:
class Test
def method(arg1, arg2)
return arg1+arg2
end
memoize :method
end
And I want to memoize it's results. So for debug purposes I modified the class like this:
class Test
def method(arg1, arg2)
puts 'sth to make sure the method was executed'
return arg1+arg2
end
...
end
...
Hello,
my rails application generates lots of small sqlite databases using DataMapper. After data saved, .sqlite-file must be uploaded on a remote server and destroyed locally.
My question is how to make DataMapper close .sqlite db connection and free repo's memory? Application should generate many databases, so it's important to save ...
C/C++ would be good option to write some of the performance critical aspects of a Ruby Application. I know this is possible.
I would like to know how to add C/C++ code into Ruby code; any other language for that matter.
Are there any practical applications of this which you noticed in open source projects or else?
...
Good afternoon all,
I've got a controller running a before filter with an except override. It works fine for all the methods in the controller, save one. I've copied the code for the method in question from another part of my app, just making the changes I need to get it working for this particular area.
Here is an example of the con...
When I try to run even simple rails commands such as:
rails -h
I get a popup error after a few seconds that says:
ruby.exe - Unable To Locate Component
This application has failed to start
because msvcrt-ruby18.dll was not
found. Re-installing the application
may fix the problem.
I am running:
Windows XP (yes I know...
I'm trying to use scrubyt to scrape a page and have everything working except for a decent way of advancing to the next page of the results. The next_page approach isn't working due to the url being relative.
I figured out a simple way to do it but it all hinges on being able to use something like:
if node_exists("//div[@class='pagina...
This is the line it lands on.
@object = Object.find(params[:id])
respond_to do |format|
if @object.update_attributes(:status=>Object::PUBLISHED, :published_at => Time.now)
format.html { redirect_to :action => :publish_detail }
format.xml { head :ok }
format.json { head :ok }
#else
# flash[:e...
I have a gem, roundhouse, which is an application compiled with .NET (C#). Runs on Windows and it should run in a 32 bit process.
To set up my gemspec, I set:
Gem::Specification.new do |s|
s.platform = 'mswin32'
s.name = 'roundhouse'
s.version = version
s.files = Dir['lib/**/*'] + Dir['bin/**/*']
s.bindir = 'bin'
...
I am using ERB via console for metaprogramming (for math software). For example, I have file test.erb containing
text line before ruby
<%= 'via <%=' %>
<% print 'print' %>
<% puts 'puts' %>
text line after ruby
When I parse it by $ erb test.erb, I get the following output
printputs
text line before ruby
via <%=
text line af...
If I create two String instances with the same content separately they are identical. This is not the case with custom classes by default (see example below).
If I have my own class (Test below) and I have a variable (@v below) which is unique, ie. two Test instances with the same @v should be treated as identical, then how would I go a...
When I use IO::popen with a non-existent command, I get an error message printed to the screen:
irb> IO.popen "fakefake"
#=> #<IO:0x187dec>
irb> (irb):1: command not found: fakefake
Is there any way I can capture this error, so I can examine from within my script?
...