ruby-on-rails

Why can't I use sort_by in a model?

I have a user model and a bid model. I want the user to know what their rank is based upon a score stored as a method, i.e. "3/7" based upon user.score method. Currently, I'm trying to tuck this geek_rank method into the Bid model as: def user_rank(my_id) #Finds all bids associated with parent ticket object bids = Bid.find_by_tick...

background job vs after_save callback

I have a model called Vote that gets changed very frequently (people voting on stuff). I do other analytics after a vote is save, such as interpolating if the voter is male/female, what age etc. This results in updating counters in (adult votes, women votes etc) the same model. I wonder what's the best way to do this after save process...

Setting a class on a rails select

I need to set an HTML class to this rails select and i have tried many variations and nothing <%= add.select :state, @states.map { |s| [ s.name, s.abbr ] }, :class => 'state', :include_blank => true %> ...

Observer Field: Autofill field when selecting another field...

Here's my existing code: <% form_for(@match) do |f| %> <%= f.error_messages %> <fieldset> <p> <label>Name</label> <%= f.text_field(:name) %> </p> <p> <label>OR email address</label> <%= f.text_field(:email) %> </p> </fieldset> <p> <la...

association sorting

I have 3 models GrandPa, Pa, Kid GrandPa => has_many :pas Pa => has_many kids, :polymorphic => true #dont ask why When I list GrandPa I would like to present the following 1 - GrandPa_Name 2 - List of Pas sorted by the number kids each pa has in descending order How do I accomplish #2? Thanks ...

How to call a method and send new parameters in RoR?

Let's say I have methodA def methodA note = Note.find(params[:id]) note.link = params[:link] note.linktype = params[:linktype] note.save redirect_to(notes_url) end When I call this method from a view like this, it works fine <%= link_to image_tag(w.link, :border =>0), methodA_path(:linktype => w.linktype, :lin...

rails interview question

I got this question in a previous interview and couldnt do it , any idea? What does this return? Where would it be used? module ApplicationHelper def show_flash flash.map{|key, value| content_tag(:div, value, {:class => key})} end end ...

The Rails Way for Reducing HTML Duplication

In my .html.erb file, I have a lot of lines of information to display. I am accomplishing this with a simple: (using blueprint css) <% for event in @user.events %> <div class="span-5 border">Descriptor:</div> <div class="span-5 last"><%=h event.foo%></div><br/> <% end %> How can I make it so that I can call some function that would re...

How to upload images in Rails?

Hello! I am looking for a tutorial/how-to for uploading and storing images in rails. I searched google, but all I found was "...use imagemagick" or stuff like that. But I did not find any step by step tutorial (including installing imagemagick or a different image-plugin). Can you give me a step by step tutorial (including installing th...

Two alias_method_chains in Plugins

In my rails application, I have two separate plugins that both call a alias_method_chain on a core method. Core Rails def some_method In PlugIn1 def some_method_with_extra_feature_one some_method_without_feature_one end alias_method_chain :some_method, :extra_feature_one In PlugIn2 def some_method_with_extra_feature_two some_...

Regular Expressions

Explain this regex used in RoR /\A([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})\Z/i What does the \A tag do ? ...

For devs who are using mongodb to build their web apps, what do you do about the long primary keys?

The ids in RDBMS's are usually simple integers that go from 0 to whatever number, but typically you can keep them in the 5 digit range so that you can make urls that look like this myawesomeblog.com/posts/23456 but with mongodb the unique identifiers for each record look like this. 47cc67093475061e3d95369d and building and app based o...

Including .rb file on view

i am including my own .rb(HintsHelper.rb) file on view. But it gives the following error. Why? uninitialized constant ActionView::Base::CompiledTemplates::HintsHelper then... i included HintsHelper.rb in Application Helper file. but still it is giving me the following error. uninitialized constant ApplicationHelper::HintsHelper How to...

Problem with HABTM and join table in Rails

I have a simple model: class User < ActiveRecord::Base has_and_belongs_to_many :roles end class Role < ActiveRecord::Base has_and_belongs_to_many :users end I have created a simple join table: class CreateUsersRoles < ActiveRecord::Migration def self.up create_table :users_roles, :id => false do |t| t.integer :user...

"uninitialized constant" when running RSpec tests with Rails 2.3.8 and Bundler

I just changed my Rails 2.3.8 project to load gems using Bundler, rather than the default Rails 2.3 loading mechanism. I followed the official instructions and the site runs fine in development. I use RVM for gem management, and have a specific gemset loaded for the application. My RSpec test suite is unable to run, however. I have trie...

nested forms with additional non-editable fields in child table

My application has got a parent and child table with a 'has_many' relationship. Now I have created a nested form as described in the tutorials (using accept_nested_attributes_for) and this works fine. Some information about my environment: Rails 2.3.8, authlogic In my child table is a additional field "user" (the reference to the curr...

Ajax validation

how to use Ajax validation on-submit event using prototype in rails application ...

Where to put repeating display code for views in a Ruby on Rails app?

I have a Note model, which can contain have either an image link attachment (linktype = "image" or some text (linktype = "text). When I display the notes, the method of display changes depending on the linktype. An example is: <% @notes.each do |q| %> <h2 class="title"><%= q.name %></h2> <% if q.linktype == "image"%> <im...

Listing available users on a certain date

I'm looking at making a booking system and I need to display all the available users on a particular day/time. Thinking about it, I'm going to set up a table to keep note of when people AREN'T available, and assume that they are otherwise available. The structure I had in mind was to keep a note of the date_start of when they won't be a...

How do I check belongs_to model attributes in a declarative_authorization file?

Hi folks, I want the check a belongs_to model for permission. A user should only create a blog, if he is the owner of the project Model Code User has_many :blogposts has_many :projects end Project has_one :blog belongs_to :user end Blog has_many :blogposts belongs_to :project end Blogpost belongs_to :user belongs_to ...