associations

Advanced associations in rails

Right now I'm using a has_and_belongs_to_many association for two of my models like so: class Books < ActiveRecord::Base has_and_belongs_to_many :publishers end class Publisher < ActiveRecord::Base belongs_to :publishing_company has_and_belongs_to_many :books end You'll notice that each publisher belongs to a publishing c...

NHibernate - How to delete a child from a bidirectional many-to-many assocation?

Hi! How can I delete a child from a bidirectional many-to-many association? Deleting the child does not work because I get an exception (FK Violation). Just removing the child from the parent and call saveorupdate on the parent does not do anything. Entities: public class Employee : Entity { public virtual string LastName { get...

nested models, validations, and deletions

After struggling with this for hours, I think I've finally developed a solution to the following problem. I have the code here: http://pastie.org/767503 . What I am doing is allowing for classic http modification of a logic expression like (OR (AND ItemsFromCollectionPredicate) (AND TruePredicate)) which is the params hash below "co...

Rails - build_association is not working for a has_one and belongs_to relationship.

I have two models class Subscription < ActiveRecord::Base belongs_to :client end class Client < ActiveRecord::Base has_one :subscription end but when I try to create a parent from the child e.g. sub.build_client the foreign key does not get set e.g. >> sub = Subscription.new => #<Subscription id: nil, token: nil, user_id: nil, c...

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

rails has_many :through has_many :through

I'm wondering to what extent I can use associations in Rails. Take into consideration the following: class User < ActiveRecord::Base has_one :provider has_many :businesses, :through => :provider end class Provider < ActiveRecord::Base has_many :businesses has_many :bids, :through => :businesses belongs_to :user end ...

Grails Many to Many Association Querying

I have a many to many relationship. class Post { String title static hasMany = [tags:Tag] } class Tag { static hasMany = [posts:Post] } I would like to get a list of posts for a tag that have some other criteria (like a sort order, partial title match, etc). Do I have to use the grails criteria to achieve this? Or is ther...

setting up associations directly through ids in grails

I have 2 domain objects with a one-to-many relationship. Just as an example: Class Author{ static hasMany = [posts: Post] } Class Post{ Author author } In the database the post table has a field called author_id. In my application, I have a bunch of author ids that I want to associate with posts. I can do this by first querying th...

:foreign_key not requesting from the right column?

I have three models: for the purpose of asking the question, I'll call them posts, containertable1 and containertable2. Basically, the posts table has a column called misc_id, which has the id of an item that is in either containertable1 or containertable2. I tried to set up a foreign key association, but it doesn't seem to fetch from...

How to get a model to specify a pre-requisite condition?

In my model "Post.rb", I'm trying to have it only return posts if column deleted is 0. In other words, when the user deletes posts, it doesn't remove the item from the database, but rather turns deleted to 1. So if I run Post.find(:all), I want it to automatically add the condition "deleted = '0'". How do I go about doing this from my...

Ruby on Rails - Accessing data in :has_many => :through association

Hi, I'm trying to get to grips with this type of association in Rails I have the following three models: User has_many: 'memberships' has_many: groups, :through => 'memberships' Membership belongs_to: 'user' belongs_to 'group' Group has_many: 'memberships' has_many: 'users', :through => 'memberships' I have a before_create metho...

Specifying the foreign key in a has_many :through relationship

I have the following three models: User, Project, and Assignment. A User has_many Projects through an assignment. However, Assignment actually has two foreign keys that relate to a User: user_id (representing the user who was assigned the project) and completer_id (representing the user who completed the project). Often, user_id and co...

Ruby on Rails - Optional Associations?

I would like to allow users to write comments on a site. If they are registered users their username is displayed with the comment, otherwise allow them to type in a name which is displayed instead. I was going to create a default anonymous user in the database and link every non-registered comment to that user. Would there be a bette...

Rails Nested Attributes Association Validation Failing

I have nested attributes for a Rails model and the associations validations are failing for some reason. I am not useing accepts_nested_attributes_for, but I am doing something very similar. class Project < ActiveRecord::Base has_many :project_attributes def name project_attributes.find_by_name("name") end def name=(val) ...

Association Problem

I am having a problem with an association: Battalion :has_many soldiers Soldiers :has_many primaries I need to do this @bseniorleads=(@user.battalion.soldiers.find(:all, :conditions => ["seniorleader = ?", "Yes"])) then @seniorspouse=(@bseniorleads.primaries.find(:all, :conditions => ["relationship = ?", "Spouse"] This ...

[Rails] How to associate a new model with existing models using has_and_belongs_to_many

I have two models with a many to many relationship using has_and_belongs_to_many. Like so: class Competition < ActiveRecord::Base has_and_belongs_to_many :teams accepts_nested_attributes_for :teams end class Team < ActiveRecord::Base has_and_belongs_to_many :competitions accepts_nested_attributes_for :competitions end If we a...

managing associations over multiple requests in grails application

i am currently implementing a grails web-app, with a couple of complex forms where modifications on an association graph should be managed "in-memory" (that is the http session) as long as the entity or top-level domain object is not saved. e.g. top-to-bottom: Document -> Categories -> Sub-Categories ... requirement: modifications to ...

Java - Hibernate - Query - Association

Hi everybody, I have three classes -> Metadata, MetadataValue and MetadataMetadataValue: Metadata private long id; private Metadata parent; private long levelInTree; private String code; private String nameEn; private String nameFr; private String descriptionEn; private String descriptionFr; private String query; private String metada...

why is this rails association loading individually after an eager load?

I'm trying to avoid the N+1 queries problem with eager loading, but it's not working. The associated models are still being loaded individually. Here are the relevant ActiveRecords and their relationships: class Player < ActiveRecord::Base has_one :tableau end Class Tableau < ActiveRecord::Base belongs_to :player has_many :table...

Rails has_one :through association

Rails has a has_one :through association that helps set up a one-to-one association with a third model by going through a second model. What is the real use of that besides making a shortcut association, that would otherwise be an extra step away. Taking this example from the Rails guide: class Supplier < ActiveRecord::Base has_one :...