ruby-on-rails

Rails: Factories and Associations in Functional Tests

I'm trying to figure out why this doesn't work. Let's say you three models, User, Foo and Bar. In order for a bar to be created the user must first create and validate a foo object. Class User #snip! has_many :foos has_many :bars Class Foo #snip! belongs_to :user has_many :bars Class Bar #snip! belongs_to :user belongs_to :foo I'm ...

Eliminate the Join Table in HasMany Relationships in Rails

I am thinking about ways to create a Role Based Access Control system in Rails. I have seen these great projects too (among others): RoleRequirement ActsAsPermissible RailsAuthorization BoxRoom (rails file management) My question is, is it really necessary to have a join table for everything? If I one of the tables in the relations...

How do I make model data accessible across all controller methods efficiently?

I have a cart which contains items, in my controller index method I use @cart = find_cart to find my cart's items. I'm trying to make a simple cart link which contains the amount of items in the cart at the top of my application layout using: <%= @cart.items.length %> It will look like cart(2), if you have two items. Without being rep...

Is it advisable to disable some helper methods in the test environment for rails?

I have an application helper which determines the css classes (selected or unselected) and link addresses for the navbar at the top of my application helper. When I test my controllers, I get a whole bunch of errors regarding the navbar (variables return nil). Since the navbar has nothing to do with each individual controller, and it ap...

Is it possible to integrate Vaadin with Rails??? If so, how???

As i see Vaadin is a Java based UI framework. But it has some really nice set of widgets and a very good layout engine. Is it possible to integrate Rails and Vaadin? JSON perhaps?? ...

How/where to temporarily store ActiveRecord objects if not in session?

I'm refactoring a Rails-based event registration application that has a checkout process that hits several ActiveRecord models. Ideally, the objects should not be saved unless checkout is complete (payment is successfully processed). I'm not entirely certain why it would be a bad thing to serialize these objects into the session tempor...

check if the value of a field changed in a before_update filter

Hi I have a database field where I want to store my password. In a before_create filter in my model I call a encryption function and save from clear text to encrypted text. I want now to use the before_update also for encryption, but only if the value has changed. How can I write a condition for checking if a field value has changed? ...

show flow player with using AJAX

I am using flow player in my site to view the videos , Now on the index view i have all the videos . but to show that video i have made remote_link to open it on the same page I got the video on the page too , but it is not displaying , i can even save the video by clicking it , but i cant see the player at all my code is controller ...

flash_set, flash_get in PHP?

Hi, so i got told in this question: http://stackoverflow.com/questions/2291413/php-javascript-passing-message-to-another-page to use flash_set and flash_get, concept called "Rails flash".. Now can you use them in php those function? I can really find them in php library site, so im not sure.. ...

Rails, select helper, adding style

select :model, :attribute, :style => "some:style;" I'm trying to do something like this. Add style to the select helper in rails ...

get info from wikipedia

Is there anithing for getting the main description of a wikipedia's page? Like in http://en.wikipedia.org/wiki/Radiohead I want obtaing: Radiohead are an English alternative rock band from Abingdon, Oxfordshire, formed in 1985. The band consists of Thom Yorke (lead vocals, rhythm guitar, piano, beats), Jonny Greenwood (lead guitar, ke...

How to handle trivial "duplication of code" smell in Rails/Ruby

So, we all strive to reduce duplication (DRY) and other smells, and keep our code as nice and clean as possible. For Ruby code, there are a lot of instruments to detect smells, for example the quite nice Caliber service. However, it seems that I have a different definition of code duplication than the tools. I think this might be connec...

Can't get rails app to start on heroku

I'm trying to deploy a rails app to heroku, but keep getting the following error. I'd have thought that managing the postgres gems would be something heroku would handle. I've tried everything I can think of short of installing postgres on my local machine, which I'd need to do if I wanted to install the postgres gem. There's also n...

Optimizing daily ranking tabulation

Each Poem as two Votes, one as poem_id, other_poem_id, wins & the second record which is the inverse of the first. Maybe there is a better way, but I'm trying to find the poems with the highest win percent over a period of time. It's confusing because of the double records for each comparison. Should I add another table, Results, whic...

Benchmarking Rails Model Methods

Is there something similar to Ruby Benchmark within Rails? I have used Ruby benchmark in the past to compare different bits of code, but none of it was Rails related. I would like to use my application models in some benchmarking to do something along the lines of... #!/usr/bin/ruby require 'benchmark' Benchmark.bmbm do |x| x.report(...

How to implement a REST API with included data in Ruby on Rails?

Hello, I'm working on a Ruby on Rails app which has a REST API. I manage two formats JSON and XML. Very often to simplify the use if the API, I'm making includes. But when you add params to to_json and to_xml like :only, :except, it's not only applied on the root object but on all the objects included. Do you know libs or methods to an...

add method to reflection-object and named-scopes

I Like to add a method to my has_many relation in the way that it is applyed on the relation object. I got an Order wich :has_many line_items I like to write things like order.line_items.calculate_total # returns the sum of line_items this I could do with: :has_many line_items do def calculate_total ... end end but this w...

How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block?

I have a standard Rails 2.3.5 app with a model called Post. Post has an attribute called url, and the following getter is defined: def url p = 'http://' u = self[:url] u.starts_with?(p) ? u : "#{p}#{u}" end If I load up script/console, I can do Post.first.url and get the desired result (e.g. it returns http://foo.com if the attr...

String to method/function call in ruby?

Hi I want to make my rails controller more flexible and try to create some Meta foo for it. I have a problem with the redirect_to method. Can I convert the "edit_admin_post_path()" method from a string or better read out the controller name and pass it dynamicly? this is my code for "post" in my Admin::Posts controller. respond_to do...

Problem in RoR testing

I reading/learning "agile develpment.." now i'm on Chapter 14 (testing)/ when i tried to run "@ruby -I test test/unit/product_test.rb" i have error that 1) Error: test_invalid_with_empty_attributes(ProductTest): ActiveRecord::StatementInvalid: PGError: ERROR: relation "carts" does not exist LINE 1: DELETE FROM "carts" ...