ruby-on-rails

Force controller to use current_user with mocking

I am trying to specify in my RSpec tests that my controller should use current_user.projects.find() instead of Project.find() I am using the Mocha mocking framework and was trying something like this: controller.current_user.projects.expects(:find).returns(@project) I have already mocked out controller.stubs(:current_user).returns(@pr...

Find or Create Multiple Records in Ruby on Rails - "find_or_create_all"

What is an efficient and elegant way of performing a "find_or_create_all" in rails? Given: names = ["Bob","John","Jack","Sid"], Users table Needed: users = ["#User:0x3ae3a00", "#User:0x3ae3938", "#User:0x3ae3898", "#User:0x3ae37f8"] where a user is created only if it doesn't exist. An intermediate solution - ["#User:0x3ae3a00", ni...

Rails -- Find with 3 conditions?

Hey, I got a find condition that is a bit lengthy and I'd like make it lengthier. SO far :conditions => ["name LIKE ? OR description LIKE ?", "%#{params[:search_keyword]}%", "%#{params[:search_keyword]}%"] Where it's searching the model for if the attribute :name or :description is like a param from a text box. Now I'd al...

Hierarchical database "next in sequence" with Rails?

I am building a site that has some ordered, hierarchical data stored in the database. There's a "Volume" table, a "Book" table that has volume_id and position fields, and a "Chapter" table that has book_id and position fields. What I'd like to build is a browser that steps through the sections. So, if I'm rendering the details about V...

Limit each do loop

If I have the following, <% @feed.sort_by{|t| - t.created_at.to_i}.each do |feed| %> <% end %> How can limit it to only show the 10 most recent results ...

Running a ssh tunnel from a rails/passenger server to another machine

I have a Rails (2.3.8) application that will need to start and maintain a SSH tunnel whenever the application is started using 'script/server' or when started using Passenger. When script/server is ^C'd or the Passenger instance is shut down the SSH tunnel should be destroyed. I do not want the tunnel to be started when I run 'script/co...

rails 2.3.5 with ruby 1.9.1p429 : incompatible character encodings: ASCII-8BIT and UTF-8

Hi, I tried the ruby hacks for utf8 (from : http://gist.github.com/273741) ... and I'm still getting the following error: ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-8) What is bizarre for me is that the same content if retrieved with a post action (searching the app with an html from) it is display...

How to to store production data to a reporting DB in a Rails app ?

We want to keep track of production data of a Rails app, so that we can analyse them in a few months. To be more precise, we have a few data Models with a "status" and dates column that can change frequently. The idea is to write somewhere a new line in a DB or log file every time the status or any other attribute changes. These line ...

Auto generate HTML element IDs in Rails when repeating the same control through partials

Hello, I am using partials in Rails combined with jQuery child field templates to dynamically generate forms with multiple nested child attributes. Within these partials is JavaScript that is fired when various events (e.g. onChange) occur in the form, and the code that executes needs to change the DOM for a related but different form el...

Delayed Job not processed in rspec

I am trying to run rspecs for a custom delayed job (GetPage::GetPageJob), but I have a problem. When I run them, the jobs are well enqueued (that is to say, well inserted in the delayed_jobs table), but they are not processed by the job worker. Indeed, after launching "rake jobs:work RAILS_ENV=test" in a first terminal, and after runnin...

invalid byte sequence in US-ASCII (Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper)

My setup is: linux + Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper I followed http://railscasts.com/episodes/194-mongodb-and-mongomapper, that everything is OK first. I can insert English strings successfully, but when I insert some Chinese strings, it inserted, but can't be displayed. The web page shows an exception: invalid byte ...

How to make 'Shoulda' test output easier to read and in Colour!

I currently working on a project which uses Spree Cart and which has hence forced me to switch from testing with RSpec to testing with Shoulda. I really like the output I get from rspec and am wanting to get similarly readable output using Shoulda. Specifically how do I achieve similar output as I would achieve with the rspec command ...

How to configure ruby on rails with Apache

Hello All, i have strugle in connection between J2me and ruby on rails under Webrick server... dats why i need configure with apache..i saw some tutorials about configuring..but it's not working.. ...

Is there a way to break out of the default_scope when using has_many?

I have a tree-like model where in all situations but one, I want to scope the results to only return the roots. class Licence < ActiveRecord::Base default_scope :conditions => { :parent_licence_id, nil } belongs_to :parent_licence, :class_name => 'Licence' has_many :nested_licences, :class_name => 'Licence', :foreign_k...

ruby on rails does update_attributes protect against sql injection?

Does update_attributes protect against sql injection? Example: if @user.update_attributes(params[:user]) # updated end I know find(), and {} and [] do in find :conditions, but didn't see any info on this method. ...

rails is Model.new safe from sql injection?

Does ModelName.new protect against sql injection? Example: @user = User.new(params[:user]) @user.save I've read the rails security docs and didn't see anything about inserts via Model.new. Thanks! ...

Change view dynamically from controller - Progress bar with threading in Rails

I have a time taking task in my controller for which I want to trigger a simple progress bar/progress status in my view. In my controller I have something like threads = [] total.times do |x| Thread.new { the_time_taking_task(x) #Something here to trigger the progressbar in the view } end threads.each { |aThrea...

rails: does the build method protect against sql injection

Does build protect against sql injection? Example: @post = @user.posts.build(params[:post]) @post.save Didn't see build in the rails security docs. Thanks! ...

Is there a way to hide some of nested objects in model?

I have some orders with nested items and the items have nested kinds. When i do a form_for @order in a view, then i would like to hide all the items that have their :registered attribute set to true <% form_for @order do |f| %> <% f.fields_for :items do |ff| %> <%# show all the items that have :registered == false %> <% end %> <...

use rails fixture object in another fixture file

I would like to use an object within a fixtures-file that was instantiated within another fixtures file. Something like the following (which is not working): monitor_france: objecttype_id: 2 name1: i-france-1 name2: <%= monitors(:big_brother).name %> In case you wonder why I try weird things like this: I'm dealing with a legacy ...