activerecord

How to get an eager loaded conditional to be lazy loading later on with ActiveRecord and Rails

First let me show some code. class User has_and_belongs_to_many :roles named_scope :employees, { :conditions => ["roles.name = 'Employee'"], :include => :roles } end OK, so later in a controller I wanted to search for all the employees right. I set the named scope up to help do that with the join and conditional search. But the pr...

Grouping by week/month/etc & ActiveRecord?

I'm doing some statics calculation in my product. A user has performed a number of operations, let's say posted comments. I want to be able to show them how many comments they've posted per week for the past month, or per month for the past year. Is there any way with activerecord to group this way? Is my best best to simply do thi...

proxy_reflection equivalent on ActiveRecord::Base#find ?

I'm trying to make a helper which automatically picks the correct partial based on the types of objects returned by either ActiveRecord::Base#find or an association. Unfortuneatly I can't just look at the first element of the returned array because I want to pick the correct one in this case as well. If you call an association, it return...

ActiveRecord - Acts_as_translatable - alias_method issue.

Hi, I'm using a rails plugin call acts_as_translatable. The plugin modify the sql sent to the DB to enable multiple translation a the model level. The plugin works good with SELECT statement, through the override of the ActiveRecord::Base::construc_finder_sql method. However, it does not work for update statements. I am tring to fix i...

ROR- Cannot use Find in a 1-many relationship

In Ruby on rails, our model includes orders and payments. There's 1-many relationship between order and payments. In the orders model we specify: *has_many :payments, :as => :payable* And a payment record has payable_id that is set to order.id. In a report, I want to select all payments that belong to orders of a given type. Using: ...

Are any alternatives to the Active Record in Rails?

Are any alternatives to the Active Record in Rails? ...

Which should I use in my delete action: user.delete or user.destroy?

I find it confusing that there is an ActiveRecord delete as well as destroy. In my Controller I want to delete a user in my delete action. The result would be that this instance represented by the User model no longer exists in the database. Which method should I use for this? ...

ActiveRecord query

I have the following ActiveRecords class Product < ActiveRecord::Base has_many :reviews end class Review < ActiveRecord::Base belongs_to :product end Each review object contains a field named 'rating' I wish to get a list of all products whose average rating is larger than a specific bound. I don't understand how to us...

Variable field name in named_scope?

In a Rails model I am trying to acheive a named_scope that filters on a start_date and end_date. This is easy. But I am going to have to do it on lots of different fields on many occasions. Is this asking for trouble? If so why (SQL injection?) and is there another way to acheive this. named_scope :between, lambda {|start_date, end_da...

The state of LINQ to NHibernate ActiveRecord

Hi, Does anyone know if the LINQ to NHibernate ActiveRecord is ready for production? I'm about to start a project that has some tight modelling deadlines. ActiveRecord looks like a winner as opposed to my previous experiences with LINQ to SQL. LINQ to SQLs db first is nice, but somewhat cumbersome after a while. What makes LINQ to SQL ...

Why is this reference to an ActiveRecord association failing?

In Rails 2.2.2 In my model I have this: class Question < ActiveRecord::Base set_table_name "t346128_question" set_primary_key "question_id" has_many :sections, :order => 'position, section_id', :dependent => :destroy ... end And in my controller, this: def answer() @question = Question.find(params[:id]) puts "question=#...

Why does Rails refuse to use my Model (based on a SQL view)?

I've create a view in MySQL which corresponds to some reports I'm generating (sums grouped by month, year with a roll up). The view has 3 columns (year, month, total). View/Table name is "report_monthly". I know I can use raw sql via connection().select_all(...) but I'd like to create an ActiveRecord for this table. Here is my model in ...

How can all ActiveRecord attribute accessors be wrapped

I'd got a model in which attributes are allowed to be null, but when a null attribute is read I'd like to take a special action. In particular, I'd like to throw a certain exception. That is, something like class MyModel < ActiveRecord::Base def anAttr read_attribute(:anAttr) or raise MyException(:anAttr) end end that's all fi...

What patterns to use to build layers for delphi win 32 application

I want to develop mysql database application using dbexpress to develop from scratch or work with existing databases. To create reusable layers what patterns-components should I use. I want the app to be scalable to n-tier easily. Tried google search for ready frameworks but I found nothing much informative (some lack documentation, some...

RoR table inheritance?

Scenario: I have a users table in my application. I also have two subclasses of users, lets say contributors and viewers. Each user group must have an entirely different set of attributes but they both share certain user properties (login, password, name). What is the best way to implement this using Ruby on Rails? I think single ta...

Implicit Tables in Ruby on Rails

I'm just learning how to program in Ruby on Rails, and I'm really impressed by how much work the Ruby on Rails framework does for you. I have the following two classes: Object -------- object_id description ObjectGroup -------- group_id description The idea is that I have some objects, and I'd like for the users to be able to define...

Group and count in Rails

I have this bit of code and I get an empty object. @results = PollRoles.find( :all, :select => 'option_id, count(*) count', :group => 'option_id', :conditions => ["poll_id = ?", @poll.id]) Is this the correct way of writing the query? I want a collection of records that have an option id and the number of...

accepts_nested_attributes_for child association validation failing

Hi all, I'm using accepts_nested_attributes_for in one of my Rails models, and I want to save the children after creating the parent. The form works perfectly, but the validation is failing. For simplicity's sake imagine the following: class Project < ActiveRecord::Base has_many :tasks accepts_nested_attributes_for :tasks end clas...

Storing sort order for items held in an HABTM association - CakePHP

In an ActiveRecord (CakePHP flavored) setup I have a HasAndBelongsToMany association setup on Videos and Bins: A Bin can store n references to Videos, and Videos can belong to n Bins. I need to be able to manually set and store the display order of the Videos within a particular Bin (so the client can have his Videos in a particular ord...

Filtering ActiveRecord queries in rails

I'm used to Django where you can run multiple filter methods on querysets, ie Item.all.filter(foo="bar").filter(something="else"). The however this is not so easy to do in Rails. Item.find(:all, :conditions => ["foo = :foo", { :foo = bar }]) returns an array meaning this will not work: Item.find(:all, :conditions => ["foo = :foo", { :...