ruby

Ruby on Rails: check the amount of products a shop owns

I'm messing around with a test/exercise project just to understand Rails better. In my case I have three models: Shop, User and Product. A Shop can be of three types: basic, medium, large. Basic can have 10 Products maximum, medium 50, large 100. I'm trying to validate this kind of data, the type of Shop and check how many products it...

Common refactoring pattern for Rails model.

Is there a pattern to refactor such a construct into a readable single line? def show_section @news = News.all_active @news = @news.where(:section => params[:section]) unless params[:section] == "all" @news = @news.all end I use Rails 3 and Ruby 1.9.2 ...

How to test when creating Ruby gem ?

I am making a gem, but how to test(not unit test, just make sure the program is right) it ? Need I pack it and run every time when I modify it? ...

Ruby free invalid pointer

When try to run my Rails application (either by running rake, ./script/console or ./script/server), I get src/tcmalloc.cc:353] Attempt to free invalid pointer or Segmentation fault (depending on if running on REE or MRI). It's been running just fine till yesterday, when with no particular reason (did not install any new gems, did not upd...

Refactor ruby helper method

I have a helper method which checks whether the collection of objects is empty? If not then it checks each one to make sure that the the existing event_id is not the @current_event.id. Here is my crack at it: def build_answers(question) if question.answers.empty? return question.answers.build else question.answers.each do |...

Using non-English controllers in rails3

I have to following problem: If i want to follow the Rails naming convention i have to use the plural version of the model's name as my controller name. Example: rails g scaffold_controller Content In this case i have a model(Content) and the previous command is going to generate a controller with the name Contents. So what if I ha...

OpenSSL.NET Porting a Ruby example to C# (From RailsCasts 143 paypal-security)

I am following through the RailsCasts episode on PayPal security. I am try to port this code to C# and am using OpenSSL.NET Also is it possible to do this without using the OpenSSL wrapper library as that uses some unmanaged code? The ruby code that I am trying to port is this: def encrypt_for_paypal(values) signed = OpenSSL::PKCS7:...

Rails validating virtual attributes.

I this model: class Bunny < ActiveRecord::Base attr_accessor :number validates_presence_of :number validates_numericality_of :number end Whenever I submit a form to create this model I get the following error: undefined method `number_before_type_cast' for #<Bunny:0x103624338> ...

Rails 3 invalid multibyte char (US-ASCII)

I found a similar post here but I can't solve the problem anyway. I got this /home/fra/siti/Pensiero/db/seeds.rb:32: invalid multibyte char (US-ASCII) /home/fra/siti/Pensiero/db/seeds.rb:32: invalid multibyte char (US-ASCII) /home/fra/siti/Pensiero/db/seeds.rb:32: syntax error, unexpected $end, expecting ')' ... ed il valore della vita,...

"wrong number of arguments" ArgumentError when using round

I am trying to convert a temperature from Fahrenheit to Celsius: puts 'Convertir grados Fahrenheit a Celcius' STDOUT.flush x = gets.chomp aprox = (x * 100.0).round(2) / 100.0 resultado = (aprox-32)/1.8 puts resultado I use the correct formula for converting Fahrenheit to Celcius: Celsius = Fahrenheit - 32 / 1.8 However, when ...

Getting a list of classes that include a module

I have a mixin for which I would like to get a list of all the classes that have included it. In the mixin module, I did the following: module MyModule def self.included(base) @classes ||= [] @classes << base.name end def self.classes @classes end end class MyClass include MyModule end This works pretty well: ...

adding the id to the hash

Ok so i am creating an hash and i need to add the id to the hash..here is my code h = Hash.new {|h1, k1| h1[k1] = Hash.new{|h2, k2| h2[k2] = []}} @result, today = [ h, h.dup], Date.today Request.find_all_by_artist("Metallica", :select => "DISTINCT venue, showdate, LOWER(song) AS song, id").each do |req| # need to insert the req.id in ...

Rails naming conventions for models with forbidden names

Hi, I'm writing a rails application, and I need to name one of my model Test, and inside that model I need a class attribute - when I tried the first one, I couldn't run any UT since Test ceased to be a module - so I ranamed to Tst. I haven't even tried naming a column class - I went with clss. What names would you use? Are there any c...

Routing Error - No route matches "/" ?

Hello, new to Ruby on Rails, trying to configure the home page of my application. I don't know what I'm doing wrong, or how to configure the :home controller, but I get this error with this routes.rb file. Routing Error - No route matches "/" Here's routes.rb SchoolCMS::Application.routes.draw do devise_for :teachers, :admin res...

doesNotUnderstand for JavaScript?

Is there a way to simulate Smalltalk's doesNotUnderstand or Ruby's method_missing in Javascript? ...

Is it common for a language to evalute undefined as equal to false? If so, why is this done?

UPDATE: Question still unanswered. @Alastair_Pitts: Unless I'm missing something, it's a two part question. The second part, "If so, why is this done?" and not been answered. Believe the question is clear, but if you have any questions -- just let me know. Thanks! undefined = unknown and is a reference to system based on ternary logi...

Storing configuration variable in ruby on rails

I'm storing configuration variables for different environments in the production.rb and development.rb production.rb ENV['my_variable'] = 'val1' development.rb ENV['my_variable'] = 'val2' Maybe exists another way to store variables for different environments. What is the best way? ...

reset my gem enviorment

I am currently unable to install any gems because I keep getting the follwiing error message ERROR: While executing gem ... (Errno::E045) Operation not supported - /home/rocker I am using sudo command. I believe the issue is in my gen env has info that is no longer correct RubyGems Environment: RUBYGEMS VERSION: 1.3.7 RUBY VERS...

how to embed a linux bash terminal inside ruby on rails web page?

how to embed a bash prompt/terminal inside ruby on rails web page? how to execute a linux command from the web page, and get the output of the ommand? ...

What is the difference between /it/ and /\Ait\Z/

In Ruby both expressions seem to do similar things: 'it' =~ /^it$/ # 0 'it' =~ /\Ait\Z/ # 0 # but /^it$/ == /\Ait\Z/ # false So I an wondering what is the difference between ^-\A and $-\Z and how to choose which one to use? ...