has-many

NHibernate explicit fluent column mapping

I have a set of fluent object mappings that looks like this: public class UserMap : ClassMap<User> { public UserMap() { Map(x => x.Id); Map(x => x.Status); } } public class SpecialUserMap : SubClassMap<SpecialUser> { public SpecialUserMap() { Map(x => x.Property); } } public class Direct...

Relation between two single-table inheritance models

Hi, I have the following two models class ContactField < ActiveRecord::Base end class Address < ContactField end class Phone < ContactField end and class Contact < ActiveRecord::Base end class Company < Contact end class Person < Contact end I want one contact, no matter is it Company or Person, to have many ContactFields(Addre...

Problem with has_many :through Association in Ruby on Rails

Hi, I've got a problem with a has_many :through association, i cant call the u1.UsersProfileAttributes.find_by_ProfileAttribute_name("icq") method, rails means that this method doesn't exist. The method u1.UsersProfileAttributes.find_by_ProfileAttribute_id(3) works correctly. u1 is a user object. I don't know whats the problem, because ...

has_many association problem with non-traditional data model...

Hi there, this is my first time posting here, looks like a great site! I'm struggling with a has_many association. I have a diary application. The Model players are as follows: User UserFriend UserFoodProfile I want to be able to get at all the foods that a user's friends have eaten. So, I want to be able to get: current_user.fri...

Why does ActiveRecord has_many use delete_all instead of destroy_all?

I have a model which has many children. I was setting/removing the children as such: mymodel.children_ids = [1,2,3] mymodel.save #add the children mymodel.children_ids = [1] mymodel.save #remove children 2,3 This works just fine, but I just realized that none of the callbacks (i.e. after_destroy) are not being called on the children m...

Find all Products by Tag in Rails with ActiveRecord

This is probably something very simple but I'm looking for the optimal way of retrieving all Products by Tag, so to speak. This is with Spree, so I should stick to the way they have modeled their data. It's actually Product and Taxon (like category, brand, etc.) So if Product has_and_belongs_to_many :taxons and Taxon has_and_belongs_t...

Rails: has_many with extra details?

While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking. A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in th...

ActiveRecord has_many where two columns in table A are primary keys in table B

I have a model, Couple, which has two columns, first_person_id and second_person_id and another model, Person, whose primary key is person_id and has the column name Here's the usage I want: #including 'Person' model for eager loading, this is crucial for me c = Couple.find(:all, :include => :persons)[0] puts "#{c.first_person.name} an...

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...

rails has_many relationship (4 models) and how to access in the view

I have 4 models: transac, transac_data, item, dvd_details class Transac < ActiveRecord::Base has_many :transac_datas has_many :items, :through => :transaction_datas end class TransactionData < ActiveRecord::Base belongs_to :item belongs_to :transaction end class Item < ActiveRecord::Base has_many :transaction_datas has...

how to define a named_scope to roll up a has_many with nested models?

First the data model: class Forum < ActiveRecord::Base has_many :topics, :dependent => :destroy, :order => 'created_at desc' end class User < ActiveRecord::Base has_many :topics, :dependent => :destroy has_many :comments, :dependent => :destroy has_many :replies, :dependent => :destroy end class Topic < ActiveRecord::Base be...

Rails validation for a has_many association

I am having trouble with validations on a has_many relationship where the children exist, but the parent doesn't. However, when creating/saving the parent object, I want to ensure that specific children (with certain attributes) have already been saved. There is a Parent object that has_many Child objects. The Child objects are persiste...

Error while using `find_or_create_by` on a `has_many` `through` association

I am running in to a problem while using find_or_create_by on a has_many through association. class Permission < ActiveRecord::Base belongs_to :user belongs_to :role end class Role < ActiveRecord::Base # DB columns: user_id, role_id has_many :permissions has_many :users, :through => :permissions end class User has_many :...

Rails: With models A has_many B belongs_to C return A where B belongs to C and B is most recently created for given A?

I have three models A, B, and C, where A has_many B; B belongs to C, and B has a created_on timestamp. C has_many A through B. I want to return all A where A has a B that belongs to C and where B is the most recently created B for that A. I can do this with a method loop. Can this be done solely with named_scopes?, or is some other ...

Rails ActiveRecord associations inconsistently updated

I am running into some Rails 2.3.5 ActiveRecord behavior I do not understand. It appears that an object can have its association ids updated in inconsistent ways. This is best explained with an example: Create a Post model with the string attribute 'title' and a Comment model with the string attribute 'content'. Here are the associa...

How to create "two-side" many-to-many relationships in Rails?

Suppose we have a photography site. Any author can subscribe to receive updates from any other author. Obviously if author A is subscribed to author B that doesn't mean that B is subscribed to A. So we build models class Author < ActiveRecord::Base has_many :subscriptions has_many :subscribed_by_author, :through => :subscriptions, :...

aggregate conditional has_many rails association

I'm not sure if this is feasible or even preferable But I'd like to build a has_many relationship or a conditional named_scope type relationship to ease my pain into one simple relationship instead of two. Currently I have two models which basically contain the same information and belong to the same Model rooms thru the same key room_...

Method for defining simultaneous has-many and has-one associations between two models in CakePHP?

One thing with which I have long had problems, within the CakePHP framework, is defining simultaneous hasOne and hasMany relationships between two models. For example: BlogEntry hasMany Comment BlogEntry hasOne MostRecentComment (where MostRecentComment is the Comment with the most recent created field) Defining these relationships in...

Ruby on Rails activerecord find average in one sql and Will_Paginate

Hi all I have the following model association: a student model and has_many scores. I need to make a list showing their names and average, min, max scores. So far I am using student.scores.average(:score) on each student, and I realise that it is doing one sql per student. How can I make the list with one joined sql? Also how would I...

[Rails] I have a has_many relationships and I want to set custom limit and offset. as well as to count them.

Hy, My code: @profile.images and i would like to get only 10 images at time and with a 10 offset, like this @profile.images(:limit => 10, :offset => 10) and not like this has_many :images, :limit => 10, :offset => 10 Then I would like to count in someway all the images for that profile. @profile.count_images Thanks (: ...