associations

When deleting hasOne or hasMany associations, should the foreignKey be set to NULL?

Given : Group hasMany Persons but the relationship is independent (ie. Persons can exist without belonging to a Group), should the foreign key in the persons table (ie group_id) be set to 0 (or NULL) when deleting a group? If you do not, the person will try to belong to a group that does't exist. The reason I ask is that this is...

Generic Java Framework to Manage Bidirectional Associations and Inverse Updates

Hello, I've been looking for a generic way to deal with bidirectional associations and a way to handle the inverse updates in manual written Java code. For those who don't know what I'm talking about, here is an example. Below it are my current results of (unsatisfying) solutions. public class A { public B getB(); public void ...

LINQ to SQL associations - how to change the value of associated field

I have 2 classes with a LINQ association between them i.e.: Table1: Table2: ID ID Name Description ForiegnID The association here is between Table1.ID -> Table2.ForiegnID I need to be able to change the value of Table2.ForiegnID, however I can't and think it is because of the association (as wh...

Are there any good professional associations for IT Managers/Programmers?

Are there any good professional associations for IT Managers that I could join and network with other managers to share ideas and information? ...

Activerecode HABTM primary key problem

I have to tables that have a many to many relationship. I have created the correct table codesecure_project_tst_definition and it works. I can join rows together by calling the codesecure_projects << method on a TstDefinition object. The problem is that for some reason active record wants to use Codesecure_project_id as the id value f...

How to achieve versioned ActiveRecord associations?

I want to work with versioned ActiveRecord associations. E.g., I want to find the object that another object belongs_to as of a certain past date, or the one that it belonged to before that. Does there already exist a library subclassing Rails' ActiveRecord to provide versioned relations? Or some other Ruby library which provides persist...

How do you run a "do" block in a Rails Model while still listing a :dependent?

So I have a User model that :has_many other models like Documents, Videos, Posts and the like. My question arises when I execute a "do" block from the User model like so: has_many :posts do def recent find(:all, :order => 'created_at desc', :limit => 12) end end This just lets me call something like user.posts.recent t...

LINQ to SQL - Nullable INT in ForeignKey = "Cannot create an association..."

I have a table that has a primary key that's an INT... I have another table, that has a foreignkey relationship to that first table, but it's a NULLABLE INT. This is perfectly ok, and 100% acceptable to SQL... however LINQ to SQL is complaining about mismatched types ("int to Nullable[int]"). Error Message: Cannot create an association...

Rails JOIN TABLES

Hi, I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN users ON users.id = articles.user_id WHERE vectors @@ to_tsquery('crime') ORDER BY articles.price ASC And I Would like to write it inside of a find method from an ActiveRecord Class named Articles (Articles belongs_to user). B...

Problem with self-referential has_many :through associations in Rails

I was reading about self-referential has_many :through data situations today, because I'm trying to build a Rails application that uses them. I found this example situation on the Internet, and I have a question about it. Let me post this example code from this guy's blog: create_table :animals do |t| t.string :species end create_tabl...

Entity Framework - how do I use the entity associations?

When I have tables in my database that have PK/FK relationships (int) and when they are modeled by the Entity Framework designer everything seems as it should be. I can write the code below and everything seems like it's going to work fine as well but then when I run the code I get an error on the project.Status.StatusName saying the Ob...

Rails: How to observe join records that don't actually have a Model?

Dear Stack, Is it possible, using an Observer, to observe the creation of JOIN records? For example, you have a User Model that has_and_belongs_to_many Book Models. Is it possible to monitor books_users records as they are created or deleted or must I have a BookUser model to do this? Example of what I want to observe: User.books <<...

Rails ActiveRecord Update question

I have a user model which has multiple addresses. Now for my application in rails, address is not mandatory. So, if someone wants to create a user and enter the address after the user has been created, my application should allow that. My problem is, for Address model I have validations for Address Line 1, City and Postal Code. These fie...

ActiveRecord has_n association

I was wondering what the best way to model a relationship where an object is associated with exactly n objects of another class. I want to extend the has_one relationship to a specific value of n. For example, a TopFiveMoviesList would belong to user and have exactly five movies. I would imagine that the underlying sql table would have...

UML class diagram association - how, when and why?

I usually get so confused with UML and this situation is no different. Let's say I have an interface IAnimal, class Food and Cat: interface IAnimal { void Feed(Food food); } class Cat : IAnimal { void Feed(Food food) { //code } } I've got 3 questions about drawing UML class diagram for these 3 elements: I assu...

How can I define a polymorphic relation between models in Django?

I am working on a Django application which contains an Offer model. An Offer instance contains the pricing conditions and points to a product definition. The product model is actually a hierarchy (I have a Television model, a Camcorder model, etc.). So I would like the Offer model to contain a polymorphic (or "generic") association to...

Elegantly selecting attributes from has_many :through join models in Rails

I'm wondering what the easiest/most elegant way of selecting attributes from join models in has_many :through associations is. Lets say we have Items, Catalogs, and CatalogItems with the following Item class: class Item < ActiveRecord::Base has_many :catalog_items has_many :catalogs, :through => :catalog_items end Additio...

Accessing associations in Rails

Is there a way that I can get a list of the models that a particular model belongs to in Rails? For example: class Project < ActiveRecord::Base has_one :status ... end class Task < ActiveRecord::Base has_one :status ... end class Status < ActiveRecord::Base belongs_to :project belongs_to :task # this is where I want to...

Better Performance on Associations

Right now I have a table called Campaigns that has many Hits, if I call say: Campaign.find(30).hits Which takes 4 seconds, or 4213 ms. If I call this instead: campaign = Campaign.find(30) campaign.hits.count Does it still load all of the hits, then count? Or does it see I am counting and avoids loading all of the hits? (Which is cur...

Why doesn't this associated create call work?

I've got a User model that has many Items. A Rating belongs to a User and an Item. In the DB, I have set ratings.user_id to be not NULL. when I am creating an Item, I would like to do this: def create current_user.items.create(params[:item]).ratings.create(params[:rating] redirect_to items_path end However, this balks wi...