activerecord

Optimized way to validate_uniqueness_of Polymorphic Join Model in ActiveRecord?

What's the best way to make 1 SQL call that checks if the join model already exists before we try to create another with this setup: class Parent < ActiveRecord::Base has_many :relationships, :as => :parent has_many :children, :through => :relationships, :class_name => "Child" end class Child < ActiveRecord::Base has_many :relati...

activerecord find conditions for associated models

so i havent found much documentation about using conditions on activerecord find methods for associated models, but i have found a variety of examples. although none of them seem to be working for me. user has_one avatar avatar belongs_to user Avatar.find(:all, :include => :user, :conditions => {:user => {:login => 'admin'}}) return...

Why did Rails ignore my user_id field?

I tried creating a DB migration in Rails that looked something like this: ruby script/generate scaffold post user_id:int title:string content:text Looking at the resulting .rb file, sure enough, I saw everything I'd entered: class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.int :user_id...

user.find(1) results in an exception, is my system ok or my setup is messed up?

My database is empty, and I understand that I should get an exception. Just want to make sure that my macbookpro is setup with rails properly. typing: user.find(1) in console I get: >> user.find(1) NoMethodError: undefined method `find' for #<User:0x1016403a0> from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.0/lib/active_model/a...

Executing Rails virtual attribute setters in order

I have an ActiveRecord model with several virtual attribute setters. I want to build an object but not save it to the database. One setter must execute before the others. How to do? As a workaround, I build the object in two steps @other_model = @some_model.build_other_model @other_model.setup(params[:other_model) Where setup is: cl...

activerecord nested conditions

this is a follow up to a previous question i asked. i am having trouble getting a query to work that has a variety of conditions for nested models. user has_one avatar user has_one profile avatar belongs_to user profile belongs_to user and i am actually able to get this to work... Avatar.find(:all, :include => {:user => :profile}, :...

Define relation between 2 tables using ActiveRecord and ASP.net mvc

I am tring to define a one to one relation between 2 tables using active record. I have a table Device and a table DeviceLocation. This is the code I use in DeviceLocation to define the relation: [PrimaryKey(PrimaryKeyType.Foreign)] public int DeviceID { get { return _deviceID; } set { _deviceID =...

ActiveRecord callback setup from different model

Let's say I have to simple models First and Second, and there is one to one relationship from Second using belongs_to :first. Now I want to do something with Second, when First is saved. But I don't want to setup an after_save callback in First, to deal with Second. I want to keep my models clean, and unaware of each other as much as po...

Filtering child objects in a has_many :through relationship in Rails 3

Greetings, I have an application where Companies and Users need to belong to each other through a CompanyMembership model, which contains extra information about the membership (specifically, whether or not the User is an admin of the company, via a boolean value admin). A simple version of the code: class CompanyMembership < ActiveRec...

Eager Load Polymorphic has_many :through Associations in ActiveRecord?

How do you eager load polymorphic has_many :through associations in Rails/ActiveRecord? Here's the base setup: class Post < ActiveRecord::Base has_many :categorizations, :as => :categorizable has_many :categories, :through => :categorizations end class Category < ActiveRecord::Base has_many :categorizations, :as => :category h...

Ruby on Rails: question about validates_presence_of

I have a relationship in my ActiveRecord based model that looks like: belongs_to :foo My model should always have foo defined in it for it to be valid. My question is, when using validates_presence of, which one is the appropriate one to use: validates_presence_of :foo or validates_presence_of :foo_id Assuming here of course, th...

Does writing has_many extensions for ActiveRecord requery the database?

If I have something like this: class Post < ActiveRecord::Base has_many :comments, :as => :commentable do def approved find(:all, :conditions => {:approved => true}) end end end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end ...when I do this, I get 2 hits to the database (not...

How to select records where a child does not exist

In rails I have 2 tables: bans(ban_id, admin_id) ban_reasons(ban_reason_id, ban_id, reason_id) I want to find all the bans for a certain admin where there is no record in the ban_reasons table. How can I do this in Rails without looping through all the ban records and filtering out all the ones with ban.ban_reasons.nil? I want to do t...

How do you say in SQL/ActiveRecord, "Find all records where attribute is nil or 'x'"?

I want to find records in ActiveRecord that have an attribute that is either nil or some value: class Model < ActiveRecord::Base end class CreateModels < ActiveRecord::Migration def self.up create_table :models do |t| t.string :some_property end end def self.down drop_table :models end end Model.all(:co...

Ways around ActiveRecord connection pool

I'm writing an Adhearsion component that uses ActiveRecord. The problem is that the component may be running for several minutes (the length of a call). During that time the component has an ActiveRecord object as an instance variable. This object uses up one database connection from the connection pool. Depending on the number of caller...

accepts_nested_attributes_for through join table with attributes on the join

How can I use ActiveRecord's accepts_nested_attributes_for helper in a has_many :through association while adding attributes to the join table? For example, say I've got a Team model: class Team < ActiveRecord::Base role = Role.find_by_name('player') has_many :players, :through => :interactions, :source...

Using Rails Form Helpers with Serialized Custom Classes

Hey, I'm trying to save a hash of options in a single DB field. The form is able to save the data to the DB but not able to retrieve it again when I go to edit it (e.g. all the other fields are prepopulated except for the wp_options fields). class Profile < ActiveRecord::Base serialize :wp_options end This is my custom class: ...

ActiveRecord find with include, conditions on the include that don't affect the parent

I have two models: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end I want to find all parents AND their children, with conditions on the children only. BUT if the parent has no children that match that criteria, I still want the parent. I tried this: Parent.all...

Is there a way to make Rails ActiveRecord attributes private?

By default, ActiveRecord takes all fields from the corresponding database table and creates public attributes for all of them. I think that it's reasonable not to make all attributes in a model public. Even more, exposing attributes that are meant for internal use clutters the model's interface and violates the incapsulation principle. ...

Is Single-Table Inheritance the right solution for my Rails problem?

Greetings, all, I'm working on an application in Ruby on Rails where we need to keep track of a bunch of external services for each user (for example, Facebook, MySpace, Google, SalesForce, Twitter, WordPress, etc) that the app will access on behalf of the user. For some services, we will need to store an (encrypted) username and passwo...