ruby-on-rails

Updating an associated record from a join table model

I have a has_many :through relationship associating players to teams THROUGH managements. I want to have a counter on the teams table (a bit like a counter cache) that tells me how many new associations there have been since the beginning of the week. Of course a counter cache wont work because it will always give all the associations t...

Find all Products by Tag in Rails with ActiveRecord

This is probably something very simple but I'm looking for the optimal way of retrieving all Products by Tag, so to speak. This is with Spree, so I should stick to the way they have modeled their data. It's actually Product and Taxon (like category, brand, etc.) So if Product has_and_belongs_to_many :taxons and Taxon has_and_belongs_t...

Named_scope in rails unique records?

Is it possible to have named_scope return records unique for a certain column? e.g named_scope :unique_styles, :order =>"title desc", :limit => 3 That will give me three styles but what if I want to make sure the title is different? In tihs case there may be three records with the same style, I want this named_scope to only give uniqu...

Using Authlogic with a User model that isn't called "User"

I have an application that tracks people, but all those people are, possibly, users of the product. I'd rather have the model called Person (rather than User), but can't seem to figure out how to indicate to Authlogic that I'm using Person instead of User. I'm sure it's somewhere obvious that I'm just not seeing, but I've been pouring o...

What is the best way to get unique elements of an array of activerecord objects based on attributes of the object?

In my application I am trying to display only unique elements of an array of activerecord objects(loss_reports) based on two attributes of a loss_report. Schema class Agent < ActiveRecord::Base has_many :loss_reports, :through => policy end class LossReport < ActiveRecord::Base belongs_to :agent end I first tried to overr...

Sending a variable through a link_to without url query in Ruby on Rails

Hi all, I'm trying to send a variable through a link_to, without using url query in Ruby on Rails, from one controller's index view to be used on another controller's index. Basically, I need to pass the Turma.id (school class id), to the Elementos_turma(peoples in the class) controller, so I can use it and filter the index: ElementosT...

Rails render if @transaction.save fails

I've been struggling with this for a while now... I have a more complex form (saves one transaction and two transaction_data at the same time). I got it to save all right, however I am struggling with the handling of errors. If I use the following in "create" - in case of an error - it doesn't hold any of the values I've had on the sam...

Activerecord Nested :include fails

I have an AR query using 'will_paginate' that looks like this: paginate :all, :page => criteria[:page], :per_page => criteria[:per_page], :include => { :user, :person }, :conditions => [conditions , criteria[:from_date], criteria[:to_date], criteria[:patient_id],criteri...

Is Ruby on Rails really MUCH faster to develop code?

I hear claims that Ruby on Rails makes web applications ten times faster to write. Is this really true? Do they just make a bunch of tools to make the simple stuff fast and the hard stuff impossible (without serious refactoring)? Can it handle difficult web problems like an advanced searches and AJAX webgrids? Can't other languages j...

[Rails] Declaring Instance Variable In View and Passing To Model

I am trying to conditionalize validation of fields being sent in a view by passing a variable from the view to the model so it knows which set of fields to check. The model looks like this: validates_presence_of :first_name, :last_name, :if => :registration_step? validates_numericality_of :primary_phone, :if => :contact_step? de...

Turn off layout for one of action

My situation: View action of ReportsController should render pure html, but not as a file (to view it in browser and save it after). So for rendering I use view template view.html.erb and i neet to turn off any layouts for this action. But in other actions of this controller layouts should stay untouched. Works only turning off for whole...

Filtering parts or all of request URL from rails logs

Rails provides filter_parameter_logging to filter sensitive parameters from the rails log. If you have a a JSONP API, some sensitive information could be present in the URL. Is there a way to filter request URLS from the log also? ...

Version Control and Deploy Rails Project with cPanel

In my server, what option i got is a basic unlimited server can host unlimited rails project possible to manage gem because it is using cpanel latest version with a ssh now my confuse is , how can i use the no ssh feature in the cpanel and ease my deployment ? i don't know is that possible to use the capistrano and git with it any guid...

Validate non-model field

I've added an extra field to my new form: <%= select_tag :quantity, options_for_select(["Select a Value"].concat((1..10).to_a)) %> It specifies the number of copies of the record to be created. How can I validate the presence (or numericality) of that field, as it's not part of the model itself? validates_presence_of :quantity fai...

Restful_authentication plugin not working

I'm using restful_authentication plugin for Ruby on Rails. All seems fine except that it seems the user session is not getting created at all. I have the create method below. It appears that the self.current_user is being set but that the actual session is never created. When and how is the current_user_session supposed to be defined...

Rails Single Table Inheritance without "type" column

I'm porting some functionality to Rails, and I'm working with an existing table which is for comments. Basically, there are two types of comments - profile comments (photo_id column is null) and photo comments (photo_id column is set to photo's ID) I got single table inheritance working just fine by adding a type field to the table, bu...

Rails nested forms

I would like to create form with text_fields TITLE CONTENT TAGS I have Post (TITLE, CONTENT) and Tag (TAGS) model. TAGS is a single text field. What do I have to do to save TAGS to Tag model. Let say I write 'banana, juice, new tag' in the TAGS field, how can this be parsed into array and then save in the Tag model. Thx! ...

Rails - How do you test ActionMailer sent a specific email in tests

Currently in my tests I do something like this to test if an email is queued to be sent assert_difference('ActionMailer::Base.deliveries.size', 1) do get :create_from_spreedly, {:user_id => @logged_in_user.id} end but if i a controller action can send two different emails i.e. one to the user if sign up goes fi...

Rails issue with reloading gems and plugins in development mode

I'm doing a little rails app for a prototype. I started off using a starterapp called basejumper After a quick start, I stumbled upon a very annoying problem. In development mode, rails crashes after the second request, i.e the first time I load a page, it works fine, a reload of the pages crashes with "You have a nil object when you ...

Rails Validating Virtual Attributes

My model looks like this: class Item < ActiveRecord::Base has_many :locations validate :validate_item_location def item_location locations.address+','+locations.city+','+locations.country end def item_location=(str) geo = Geokit::Geocoders::MultiGeocoder.geocode(str) if geo.success locations.build( :lat => ...