activerecord

Ruby on Rails: joining two tables on something other than the standard foreign key

I have a rails query like: @user = User.find_by_username(params[:search], :include => [:user_similars]) Models: user (has_many :user_similars), user_similar (belongs_to :user) The user_similars mysql table has a "user_id" field as a foreign key, but I don't want :include to join on that for this instance. I'd like to join on...

How do I access hidden columns from ActiveRecord (in informix)

Many databases have a hidden primary key column. MySQL implements this as _rowid. In MySQL's case it is really a pointer to a previously defined primary key column. However in other databases (in my case, Informix), this column is independent of a deliberately defined primary key. The database which I'm coding for was designed mostly...

Is it possible to have ActiveRecord and DataMapper use the same database in the same app?

I'm in the process of replacing ActiveRecord with DataMapper in one of my apps. Since there aren't any authentication solutions that are compatable with DataMapper, I'm thinking that I could use ActiveRecord just for user authentication, and then use DataMapper everywhere else. I'd like to have both ORMs interacting with the same databas...

Multiple database connections in Rails

I'm writing a simpler version of phpMyAdmin in Rails; this web app will run on a web server (where users will be able to indicate the database name, hostname, username, password, and port number of one of the database servers running on the same network). The user will then be connected to that machine and will be able to use the UI to a...

Rails/ActiveRecord: Return order with ActiveRecord#find(Array)

Hello, I'm using Ruby on Rails/ActiveRecord and am having trouble with an ActiveRecord#find call. I'm storing in the database a serialized array of recently viewed documents IDs. The IDs are stored in descending order of when they were last viewed, so the most recently viewed document ID is first in the array. The array includes 10 I...

before_filter or rescue to create a new record

Hi, I've 2 models class Room < ActiveRecord::Base has_many :people accepts_nested_attributes_for :people, :reject_if => lambda { |a| a[:person_id].blank? }, :allow_destroy => true end class Person < ActiveRecord::Base belongs_to :room end In '/rooms/new' form I've a select tag containing all Person + an 'other' option tag that...

setting attributes in Rails has_many :finder_sql relationship

My question is somewhat complicated, but bear with me. My project has a number of models: User, Program, and Team. Users belong to multiple Programs. Programs have multiple teams, but Teams belong to one program. For each Program a User belongs to, he can belong to multiple Teams. (Think of this in an athletic context where users ar...

Name a reference in ActiveRecord

in my rails app, I have an Organization model, with a reference to a User, which I want to be able to access via organization_instance.adminstrator. I'm not sure how to accomplish this using the belongs_to method. ...

ActiveRecord model with datetime stamp with timezone support attribute.

Rails is great that it will support timezone overall in the application with Time.zone. I need to be able to support the timezone a user selects for a record. The user will be able to select date, time, and timezone for the record and I would like all calculations to be done with respect to the user selected timezone. My question is wha...

Which is the best database strategy for a JRuby on Rails + legacy Java code?

We have a medium size Java application that needs some refactoring. We are considering migrating towards JRuby on Rails. Mainly because of the productivity that Ruby on Rails offers and for the many existing plugins that will reimplement the web logic. However a large part of the application should stay in Java, we do not want to rewri...

Rails complex form editing multiple records at once with associations

I am developing a complex form that updates several records of one model at once, whilst simultaneously updating an associated model. It looks a bit like this: class Sport has_one :photo end class Photo belongs_to :sport acts_as_fleximage end class Page # the page is not related to either of the previous models end Just for ...

Which DAL choice should I use?

I'm working on a little side project. I've got a large SQL query expression, 30+ lines, that I wish to use in my project. This app needs to provide read-only access to the database through these queries. There are so many data layer choices. nHibernate, Entity Framework, LINQ to SQL, Datasets, Castle ActiveRecord... others I can't na...

Subsonic 3 ActiveRecord Delete/Destroy throwing strange exception

...

Rails truncates hash on save:

I have a rails model with a hashed password field in it (surprise, surprise), which after some manipulation, is 40 characters long. I generate a user in script/console and it appears as follows: #<User id: 1, firstname: "true", lastname: "false", username: "chaines51", hashed_password: "2Gr0GWvPunB3x5jomRTSTZJRIelC2RW103d7f3db"> I the...

sql injection prevention for create method in rails controller

As seen in comment_controller.rb: def create @comment = Comment.new(params[:comment]) @comment.save end Im assuming that this is SQL injection-unsafe. But what is the correct way of doing it?.. All the examples on the net deal with finds. ...

Return entity via projection query

Is it possible to return an entity using a projection query? I've successfully done it with a SQL query (see below), but can't find how to do it with a projection query. Dim sql As String = "SELECT {a.*}, {b.*} FROM a LEFT OUTER JOIN b ON a.pk = b.fk") ' Convert SQL results into entities {a} and {b} Dim query As IQuery = session.Creat...

Finding gaps within date-ranges in an associated model | Ruby on Rails

Given the following models: class Room < ActiveRecord::Base has_many :contracts end class Contracts < ActiveRecord::Base # columns # start_date Date # end_date Date belongs_to :room end The contracts of one room are not overlapping. My question is, how i am be able to find gaps between contracts. An Example: room = Room...

has_many :through a has_and_belongs_to_many association

I am trying to do the following in a Ruby on Rails project: class FoodItem < ActiveRecord::Base has_and_belongs_to_many :food_categories has_many :places, :through => :food_categories end class FoodCategory < ActiveRecord::Base has_and_belongs_to_many :food_items belongs_to :place end class Place < ActiveRecord::Base has_m...

How to handle revisions to a Wiki with ActiveRecord?

I have a model Article, that has_many Revisions. Revisions have a variety of columns storing all the information about the Article. The Article also belongs_to a current_revision, which is the primary key of the Revision that is currently selected. Each Revision is never changed after being created. When a user goes to edit an Articl...

How do I add a count property to an item in a collection in Rails?

I have a data model involving Users and Awards, and joined by a user_awards table. class User < ActiveRecord::Base :has_many :user_awards :has_many :awards, :through => :user_awards # awards the user has won end class Award < ActiveRecord::Base :has_many :user_awards :has_many :users, :through => :user_awards end I'd like th...