activerecord

HOWTO rank items by balance in Ruby on rails

I am implementing a betting system and each user has a balance , how can i find a rank of a user using activerecord methods? thanks for your help. ...

Obfuscating ids in Rails app

I'm trying to obfuscate all the ids that leave the server, i.e., ids appearing in URLs and in the HTML output. I've written a simple Base62 lib that has the methods encode and decode. Defining—or better—overwriting the id method of an ActiveRecord to return the encoded version of the id and adjusting the controller to load the resource ...

A rails migration that specifies the distance between two months..?

I would like to make two drop downs. a start time, and an end time. Specifically, I only need months. I would like to, for example, choose January, and then March, and then have the database read that it is the these two months plus February. Is there any out of the box migration that could work? I'm guessing.. script/generate migrat...

ActiveRecord :through to set default values on through table.

I would like to set a default value in a has_many through association. Lets say I have three models: People Friends Dogs A person can request that a dog becomes their friend. So a person would create an association where friends has an active column = false. User has_many :friends has_many :dogs, :through => :friends Now whe...

Creating relationship between two model instances

This is probably pretty simple, but here: Say I've got two models, Thing and Tag class Thing < ActiveRecord::Base has_and_belongs_to_many :tags end class Tag < ActiveRecord::Base has_and_belongs_to_many :things end And I have an instance of each. I want to link them. Can I do something like: @thing = Thing.find(1) @tag = Ta...

How can I order my entries by sum from a separate table?

I am wondering how I can order posts in my PostController#index to display by a column total in a separate table. Here is how I have it set up. class Post < ActiveRecord::Base :has_many :votes end and Class Vote < ActiveRecord::Base :belongs_to :post end I user can either vote up or down a particular post. I know there are li...

How does Active Record pattern go a long with a Unit of Work pattern

I'm working on extending a framework currently implementing an Active Record pattern. The implementations provides a store/save method on the ActiveRecord class. I guess much of this implementation would be the responsibility of the unit of work implementation, like figuring out when and what object relations to save and so on? How doe...

Rails 2.3 uniqueness validation - how can I capture the value causing the error

Hi - I'm trying to capture the value that's throwing a uniqueness error (or for that matter, any other type of built-in validation) to display it in the :message option. Here's what I tried (didn't work) # inside the model validate_uniqueness_of :name, :message => "#{name} has already been taken" # also tried using #{:name} I could us...

In Rails models; for symbols get automatically converted to YAML when saving to DB. What is the correct approach?

In my model example Game, has a status column. But I usually set status by using symbols. Example self.status = :active MATCH_STATUS = { :betting_on => "Betting is on", :home_team_won => "Home team has won", :visiting_team_won => "Visiting team has one", :game_tie => "Game is tied" }.freeze def viewable...

How to update existing record if the one being saved has the same key?

MySQL has a very nice option for INSERT statement, which is particularly helpful for join tables without the id column. It inserts a record, but, instead of throwing an error if its key clashed with the existing one, that record is updated. Here's an example: INSERT INTO table (key1,key2,data) VALUES (1,2,3) ON DUPLICATE KEY UPDATE ...

How do I verify the URL that ActiveResource intends to use on a call?

Hi there, I'm trying to request data from another Rails app, but I keep getting a "URL not found" error. Is it possible to use the debugger to figure out the intended URL and parameters for an intended ActiveResource call? For example, I'd like to make a GET request with the following parameters: https://some_server.com/employees/searc...

Ruby on Rails Associations

Hey all, I am starting to create my sites in Ruby on Rails these days instead of PHP. I have picked up the language easily but still not 100% confident with associations :) I have this situation: User Model has_and_belongs_to_many :roles Roles Model has_and_belongs_to_many :users Journal Model has_and_belongs_to_many :roles ...

Rails: Design Pattern to Store Order of Relations

Hi, I have four models: Customer, QueueRed, QueueBlue, QueueGreen. The Queue models have a one to many relationship with customers A customer must always be in a queue A customer can only be in one queue at a time A customer can change queues We must be able to find out the customers current position in their respective queue In a...

Model association changes in production environment, specifically converting a model to polymorphic?

Hi everyone, I was hoping I could get feedback on major changes to how a model works in an app that is in production already. In my case I have a model Record, that has_many PhoneNumbers. Currently it is a typical has_many belongs_to association with a record having many PhoneNumbers. Of course, I now have a feature of adding tempora...

howto have condition in a nested SQL query?

here is my SQL statement , i would like to find all the games that have the status 0 and names of teams that are like key_word or the sport's name that are like the key word. The problem is that all the games that are displayed don't have status 0 . What am i doing wrong? sql="select * from games where games.status=0 and games.team_2_id...

SQL with HAVING and temp table not working in Rails

I can't get the following SQL query to work quite right in Rails. It runs, but it fails to do the "HAVING row_number = 1" part, so I'm getting all the records, instead of just the first record from each group. A quick description of the query: it is finding hotel deals with various criteria, and in particular, priortizing them being pa...

Can a Simple Model just Extend Zend_Db_Row (essentially Active Record)?

I know Domain Models and Data Mappers are the OOP snob's choice (using 'snob' in a complementary way, as Martin Fowler calls himself one) - however, even Fowler says in POEAA that "Active Record is a good choice for domain logic that isn't too complex..." I have a simple product and invoice domain model, not too many tables/objects...

Polymorphic models hackery

Hello, Even though I know that, at least to my knowledge, this is not the default way of doing associations with ActiveRecord I'm looking for help in order to implement an "hibernatish" Polymorphic model. For instance, consider the following base model: # Content only has one property named path class Content < ActiveRecord::Base se...

Can i use a model object directly in a find

I have the following models: student class teacher a student can have multiple classes, a class can have 0 or 1 teachers. I want to be able to call a method on the student to see if they have a specific teacher, and return true or false. The following code seems to work, but i thought it looked a bit long winded, having to compare eac...

Building and submitting an Active Record form by Ajax in Rails best practices?

Alright all you wonderful people out there; In my user interface I am building a form where users can add new records via Ajax. The users will be able add multiple records from the form (it will be cleared after each post) and I am wondering what conventionally would be the best way to setup the form. If I want to use form_for helpers ...