activerecord

belongs_to association fails to load target parent referred from a setter method on mass assignment

It seems that when a child object has a reference to its parent in its setter, it fails to get initialized unless the foreign key is given first in the parameter hash. class Bot < ActiveRecord::Base has_many :items end class Item < ActiveRecord::Base belongs_to :bot def name=(text) write_attribute(:name, "#{self.bot.name}'s ...

Rails: Conditionally loading associations

Hey! I have models club and course where a course belongs to a club and and a club has many courses. When I load a club, I also want to load it's associated courses, but I only want to load those that meet a conditional test (approved? == true). It is straightforward how to do this if I were working directly with the courses: @course...

What's the Rails 3 replacement for ActiveRecord::Errors?

What's the Rails 3 replacement for ActiveRecord::Errors? In Rails 2.3.8, this is an object: >> ActiveRecord::Errors => ActiveRecord::Errors In Rails 3.0.0rc, you get a NameError: >> ActiveRecord::Errors NameError: uninitialized constant ActiveRecord::Errors from (irb):2 I'm trying to make the wizardly generator work with Rails 3....

How does Castle.ActiveRecord override virtual properties?

In active record to write a model you write: [ActiveRecord("TableName")] public class Model { [Property("SomeField")] public virtual string SomeField { get; set; }; [Property("SomeLazyField"), Lazy= true] public virtual string SomeLazyField { get; set; }; } If the field is lazy it must fetch it on the first access, so ...

ActiveRecord::Base#find returns no records in Single Table Inheritance (STI)

app/models class Amodel < ActiveRecord::Base end class Bmodel < Amodel end class Cmodel < Bmodel end db/migrate create_table :amodels do |t| t.string :type end on script/console... $ script/console Loading development environment (Rails 2.3.4) >> Cmodel.create => #<Cmodel id: 1, type: "Cmodel"> >> Bmodel.find(:all) => [#<...

Time.now in activerecord format

I need it for an unit test... how to get a time.now in the same format as active record returns for a datetime field? ...

How can I merge :conditions for my SQL query

Let's presume that I have two different conditions :conditions => ["base == ?", self.search_text] and :conditions => ["status = ?", "#{self.status}"] Is there any way to merge these conditions into one query? ...

How to validate a rails model against the original value but store the processed value

I am interacting with a time duration in a rails form, currently it is a text box and the format requires MM:SS I have the validator: validates_format_of :time, :with => /^[0-9]?[0-9]{1}[:][0-9]{2}$/, :allow_nil => true, :allow_blank => true, :message => 'format must be MM:SS' though I want to store this in the database as an integer...

Rails: Many-to-many relationships with 3 Models

Hi, I have a situation where I have Products, Suppliers, ShoppingLists and Valuations. A shopping_list consist of many valuations each with a product, an specific supplier and a price. My models are as follows: class Product < ActiveRecord::Base has_many :valuations has_many :shopping_lists, :through => :valuations end class Supp...

Why am I getting a segmentation fault with the SQL Server ActiveRecord adapter?

isql runs just fine: kobey:/etc# isql -v censored censored censored +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | ...

Multiple has_many association through same table in Rails

I have following database schema: I want to be able to do something like this: dog.head << Feature.new(...) dog.tail << Feature.new(...) I am new to Rails, so I am not always sure by 100% what I am writing, but I tried following declaration of Dog class, and failed :) : class Dog < ActiveRecord::Base has_many :features, :through...

How can I SELECT from multiple tables in CodeIgniter

This is really boggling my mind on how to do this in ActiveRecord Queries in CodeIgniter. Maybe I'm over complicating it. I have three tables: (to keep it simple I'll only show the relevant fields) Articles (id, title, text, author) Comments (id, article_id, text, author) Users (user_id, user_type, first_name, last_name, email, passwo...

Rails, how create alias of some model

Hello, i have Ruby class: class Migrator def self.migrate_old_categories ActiveRecord::Base.establish_connection(:data_center_v2) ActiveRecord::Base.table_name = "categories" end end I need use it, as i used it always. For example: Category.find(:all) So, how i can it, when i'm write: Migrator.migrate_ol...

Exception `ArgumentError' in activerecord-oracle_enhanced-adapter

Hi. I have a rails app under Oracle, Apache and Phusion Passenger. I use activerecord-oracle_enhanced-adapter-1.3.0 and ruby-oci 2.0.4. Every time I made a request, this error appear, even if the request is served correctly. Apparently nothing goes wrong, but my boss is very nervous about it and I didn't find any clue on Google about thi...

Using ActiveRecord::Dirty changes method in a model that has many relationships

Hi! I have a model that I am attempting to apply optimistic locking to. If the user has a stale record I need the system to display what they entered and ask them to try again. I have attempted to use the changes method, which works. My issue is that the model has many different levels of related models, that are all submitted within the...

Problem with eager loading of nested models

Hi, I have some models - NewsArticle, Comment, User (as :author) and Profile. class NewsArticle < ActiveRecord::Base belongs_to :author, :class_name => "User", :foreign_key => "user_id" has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at', :include => 'translations' end class Comment < ActiveRecor...

Rails object relationships and rendering to JSON question

Disclaimer, I know very little about Rails. I'll try to be succinct. ModelA belongs_to ModelB ModelB has_many ModelA The JSON Show action of the ModelA controller should show all ObjectA's that are belonging to the ObjectB of which the ObjectA in question is a member of. So if I have an ObjectB that contains ObjectA's of ID 1, 2, and ...

Rails find with three tables and a SUM operation

I'm a little stumped as to get the order of records I want with a find operation. Let's say you had three models: 1. Websites 2. Links 3. Votes A website has many links and a link has many votes. Each vote has a certain amount of points that a user can attribute to that vote. I'm trying to get a website index page where websites are li...

ValidateRegExp client-side vs server-side issue

I'm using monorail, activerecord, and jquery. I have a form with a zip code textbox. I have in my active record class associated to the form: [Property] [ValidateNonEmpty] [ValidateRegExp(@"/^\d{5}(-\d{4})?$/", "Invalid")] public string ZipCode { get; set; } As you can see, I'm using the ValidateRegExp attribut...

how to add records in a has_many :through relationship

I have two models, Groups and Employees which are related by a has_many class Group < ActiveRecord::Base has_many :groupizations has_many :employees, :through => :groupizations end class Employee < ActiveRecord::Base has_many :groupizations has_many :groups, :through => :groupizations end Question: In page view/e...