ruby-on-rails

Get the value of a model field as it is in the database

Suppose I do >> a = Annotation.first >> a.body => "?" >> a.body = "hello" => "hello" Now, I haven't saved a yet, so in the database its body is still ?. How can I find out what a's body was before I changed it? I guess I could do Annotation.find(a.id).body, but I wonder if there's a cleaner way (e.g., one that doesn't do a DB query) ...

How do attribute query methods work?

I.e., is Post.title? equivalent to Post.title.present? ...

How to model the concept of "Featuring" (i.e., when an artist is "featured" on a song)

Sometimes more than one artist will be on a song. For example, Jay-z's new song "A Star is Born" features the artist Cole, and thus would be listed as "Jay-z (featuring Cole) - A Star is Born" in a catalog. My question is how to model this in my database. Right now it's pretty simple: every song belongs_to :artist and every artist has_m...

Rails - What Should I use for Search?

I have a couple specific needs for my search and I'm interested to get people's opinions on what search approach makes the most sense. Based on my explanation below, would you recommend that I use basic sql queries? Or step up to a more advanced search solution, like Sphinx? I have two models that I want to search in: products and v...

Cannot start Mongrel Server

Hi everyone, I can't start mongrel server. When I have a look at log file, it tells me that the address that I wanted to use is not available. I tried to check if there were some services using that address, but I could not find any. The error message in log file: `initialize_without_backlog': Cannot assign requested address - bind(2)...

Loading a webpage for parsing in Rails

Assume, I want to get a page from the web to my application and make some sort of parsing with it. How do I do that? Where should I start from? Should be some plugins/gems required? What is your usual practice in resolving such type of tasks? ...

Activerecord, 'foregin_key' has to be a combination of 2 fields

I want a has_many relationship described below class User < ActiveRecord::Base has_many :mcollections, :foreign_key=>'obj_id' end Below is definition of table mcollections create table mcollections ( id int not null auto_increment, obj_id varchar(255) not null, category ...

Rails: Adding data without triggering before_save

I have a table where I am dumping some transaction data. I have a transaction model. In it, there is a before_save in which I process the transaction through the credit_card gateway and use the results to fill out the rest of the fields in the transaction model. I have a Transaction Observer watching that sends out a notification when ...

How do I make named_scope work properly with a joined table?

Here's my situation. I have two tables: pledges and pledge_transactions. When a user makes a pledge, he has only a row in the pledges table. Later when it comes time to fulfill the pledge, each payment is logged in my pledge_transactions table. I need to be able to query all open pledges which means that the sum of the amounts in the t...

Unable to read array after eval(responseText) + Javascript + JSON + Rails

I pass activerecord data from rails to JavaScript function as JSON using ajax. my request.responseText looks like this [{"site": {"lng": 55.1443, "lat": 25.0608}}, {"site": {"lng": 55.1065, "lat": 25.0399}}] Below is my JavaScript code var sites=eval('(' + request.responseText + ')'); for (var i = 0 ; i < sites.length ; i++) { ...

Is there a Google Search API designed for server-to-server communication?

I.e., I want my web server to initiate a Google Search, manipulate the results, and send the result back to the user's browser. I know there's an AJAX API designed for searching client-side with Javascript, but is there a non-AJAX API designed for server-to-server communication (like there is for maps)? ...

A problem of webservice calling in rails

In rails, as the "open" method of 'rest-open-uri' package can help call restful webservice. In a project, I can use that method to call restful webservice of other projects. However, when I use "open" method to call a restful webservice of the same project, it always returns the "execution expired" error as follows. Who can tell me why, ...

Message to a client from the server

Hi, I have to design a client/server system emulated on a website running Ruby on Rails that should work like this: a page is requested by a web browser and once it's opened the server can push messages to it I know this is not possible "naturally" but I was thinking of a sort of "java applet" that is running on that page, listening on...

Using a select field with Rails nested object forms

Is it possible to use select fields with nested object forms feature of Rails 2.3? Example: Suppose you have a Article model, Category model, and a ArticleCategories join model. Article has_many Categories through ArticleCategories. On our Edit Article form, you want to have an HTML select list of all the available categories. The u...

*args and methods that expect a specific number of arguments

Hey folks, instead of this module ActionView module Helpers module FormHelper def text_area_with_wrap(object_name, method, options) "<span class=\"wrap\">#{text_area_without_wrap(object_name, method, options)}</span>" end alias_method_chain :text_area, :wrap end end end for obvious reasons I woul...

Download an image and save it.

Hello, what's the best way to download an image and save it? My current code is: temp_file = Tempfile.new "filename", "#{RAILS_ROOT}/tmp/" temp_file.puts open(path_to_picture, 'User-Agent' => 'Test').read mimetype = `file -ib #{temp_file.path}`.gsub(/\n/,"") But it seems, that the mimetyp isn't ok. Best regards ...

How to save an associating record between has-many classes in Ruby on Rails

I've created three classes to represent Books, People, and BookLoans. While I am able to show the association of People to Books through BookLoans I've been seeding my database. I now need to save a checkout of a book. It was my intention to do this action through the book controller. Specifically, creating a loan action in the BooksCon...

Finding a label with Webrat that contains a link

So I'm doing BDD with Cucumber and have a form with checkboxes populated from a database. The labels for the checkboxes contain hyperlinks. So far, not too exotic (note, this is HAML and not Erb, but it should be readable enough for any Rails person): I would like my donation to support: %br - for podcast in @podcasts = check_box_ta...

validating that only a given number of entries are flagged

I'm really really new to rails. I haven't really written any custom validation methods and I was wondering if one exists for this or if I should write my own. I have a boolean value for images that says whether it's featured or not. How would I validate upon creating new entries and editing that there aren't more than a given number of ...

Multiple multi-line HAML blocks

Using the (intentionally) strange multi-line format for HAML, I'd like to have the following lines in my template: = call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', | :foo4 => 'bar4', :foo5 => 'bar5' | -# and = call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', | :foo4 => 'bar4', :foo5 => 'bar5' | ...