activerecord

Javascript validation using activerecord models validators

I have an activerecord model and set of validators to it, But I also have to provide front end validation. Is there a gem or a smart way to use activerecord validators, for instance in jquery validation plugin, instead of writing it by hand? Thanks ...

How to alter columns with ActiveRecord in Rails?

I'm new to ActiveRecord. I realized that I had forgotten to add a default value for a column in one of my tables. I want to create a migration to fix this, but I can't figure out how. Is there an alter_column method that you can call during migrations? If not, how can I do it? EDIT: I just tried using change_column, but this causes an e...

How can I sort by the average of a polymorphic child's attribute with ActiveRecord 2.3?

I have this schema: class Comment has_many :ratings, :as => :target end class Rating belongs_to :target, :polymorphic => true end I want to write a named scope that will sort comments by their average rating, without fetching the whole list of comments and then fetching all their ratings. I think I need to use :include and :grou...

Rails see generated queries in log

Is there a way to see all generated queries from rails in production environment like you can in development? ...

Rails-way: Group has_one Manager, Salesman, Contact, Owner, Speaker... all User, how to distinguish?

What is the correct way to model many has_one (or even has_many) relationships on a model where each has_one is just a different role/context of some class? class Group < ActiveRecord::Base has_many :memberships, :as => :membered has_one :admin, :class_name => "User", :through => :memberships has_one :speaker, :class_name => "User...

Overriding active record model attributes only in the view.

I have an object called human and that object has a field called job. I want users to be able to add their own jobs (which will obviously be added in their own language) but I want to have any job that is a default in the database to be translated when someone does a Human.job, can rails translate the default if it is in the view but not...

How to join across polymorphic tables in one query?

Hi all, I have 2 polymorphic associations through which I need to query. I have a news_article table which has a polymorphic association to teams, players, etc. Those teams, players, etc have a polymorphic association to photos through phototenic. I need to find all articles that have at least one picture that is 500px wide. The Ar...

Rails :HOw to order a table by associated model

I think the problem is trivial.I have two models: User and Betting. User has_many :bettings Betting belongs_to :user I just want to get the users ordered by who made more bettings. Sorry, for my english Thanks in advance Francesco ...

rails: filter categories where there are at least one article

Hi, I have articles and categories in a n:m relation: I looking for a find statement on the Category Model so that I can get all categories witch consist at least one article. Should be easy, but I didn't find a efficient solution, without searching retrieving all the articles. Thanks, Maechi ...

has many polymorphic images through another polymorphic model

Greets to ruby developers. I'm stuck with ActiveRecord model associations. I illustrate what I want to do by a tables. I have these tables: Products: +----+-----------+ | id | name | +----+-----------+ | 1 | Cellphone | +----+-----------+ Images: +----+-------+--------------+----------------+ | id | url | imageable_id | image...

Rails gem rails3-jquery-autocomplete: How do I query multiple fields

I'm using the rails3-jquery-autocomplete gem found here: http://github.com/crowdint/rails3-jquery-autocomplete The instructions are clear for how to query a single attribute of a model and I am able to make that work without a problem. My Person model has two attributes that I would like to combine and query, however. They are first_n...

Mysql strings only get saved up to about 277 char

Hello, Im using ruby on rails with a mysql db in the back. I realized, that the characters are limited up to about 277 per column entry... How can I increase this? Thanks, Markus ...

How is it that find_by_id, etc. work on an ActiveRecord array?

Forgive me if I've got my terminology wrong; I am still quite new to Ruby and Rails. For school I am working on an RoR project as part of a team. I was pair programming with a teammate, who actually happens to be experienced in RoR; and I wrote something like the following: d = self.deliverables.find_all_by_lifecycle_id(self.lifecycle_...

Problem with active-record and sql

Hi everybody, I have a little problem: I can't compose sql-query inside AR. So, I have Project and Task models, Project has_many Tasks. Task has aasm-field (i.e. "status"; but it doesn't matter, i can be simple int or string field). So, I want on my projects index page list all (last) projects and for every project I want count it's ac...

How to load all children of a better nested set with activerecord?

Hello, I'm using better nested set. I now want a active record statement, where I can get all top elements and include all elements of this root! How is this possible? I think there must be a possibility to include all the childrens into their root elements in one statement... Does anybody have an idea how to do this? Thanks for your...

How to use Rails 3 scope to filter on habtm join table where the associated records don't exist?

I have an Author model which habtm :feeds. Using Rails 3 want to setup a scope that finds all authors that have no associated feeds. class Author < ActiveRecord::Base has_and_belongs_to_many :feeds scope :without_feed, joins(:feeds).where("authors_feeds.feed_id is null") end ...doesn't seem to work. It feels like a simple t...

Rails ActiveSupport Issue with state gems for notifications

Hello, I have installed multiple state_machine gems to my app to use them for a notification system but every time I run into an ActiveSupport issue. It usually looks something almost identical to this: >> m = Message.new TypeError: wrong argument type nil (expected Module) from /home/Ryan/appname/app/models/message.rb:2:in `i...

Edit and delete multiple child records

Ive got projects and devices which are linked to each other in a many-to-many relation. On creation of a project the user gets a list of available devices to link to the project using checkboxes. The controller code for create is shown below. I am using ASP MVC with ActiveRecord. public ActionResult Create() { ViewData["devices...

How to save something to the database after failed ActiveRecord validations?

Basically what I want to do is to log an action on MyModel in the table of MyModelLog. Here's some pseudo code: class MyModel < ActiveRecord::Base validate :something def something # test errors.add(:data, "bug!!") end end I also have a model looking like this: class MyModelLog < ActiveRecord::Base def self.log_so...

Is attr_accessible inherited by derived classes whe using STI?

I've got a class structure similar to this for a family of classes using STI class Root < ActiveRecord::Base attr_accessible :root_prop end class Child < Root attr_accessible :child_prop end class Grandchild < Child attr_accessible :gc_prop end All my properties were working fine until I added the attr_accesible markers, so I...