associations

rails belongs_to has_one. need some explanation

I've got two models: Customer and Contact Customers table has columns :id, :firstName, :lastName Contacts table has columns :id, :cid, :hphone, :cphone So if Customers table has data 1 Josh McDonnel Then Contacts table has corresponding 5 1 947-245-2342 342-543-8585 What associations can I use here? Will Contact have...

rails: how to add child association through a text field?

Hello all, I have two models: Company and Person class Person < ActiveRecord::Base belongs_to :company end class Company < ActiveRecord::Base has_many :people accepts_nested_attributes_for :people, :allow_destroy => true, :reject_if => proc {|attrs| attrs.all? {|k,v| v.blank? } } end And my HTML form partial for new and edit a...

Selecting and Displaying Columns from Associated Tables in Rails

I am working on developing a skeleton of what to eventually become an administration dashboard. One of the things the dashboard will eventually have to do is allow users to search for users of a game and view information about them. The information about them in question is in different table so naturally I have had to employ associati...

Rails has_one vs belongs_to semantics

I have a model representing a Content item that contains some images. The number of images are fixed as these image references are very specific to the content. For example, the Content model refers to the Image model twice (profile image, and background image). I am trying to avoid a generic has_many, and sticking to multiple has_one's....

has_many with multi-level hierarchy and single table inheritance

In my Rails app I have a multi-level hierarchy of the following kind: class Vehicle < ActiveRecord::Base end class RoadVehicle < Vehicle end class Car < RoadVehicle end class Buss < RoadVehicle end Then I have a class referencing the middle level like so: class Garage < ActiveRecord::Base has_many :road_vehicles end In this simpl...

What is the convention when relevant data needs to be stored in a join table?

Suppose you have a data model that is something like class Question has_and_belongs_to_many :choices end Now suppose, on that choices model, there is a position column. What is the best way to access that information without having horrible messy queries / models? The beauty of has_and_belongs_to_many is that it keeps things concis...

Rails complex form editing multiple records at once with associations

I am developing a complex form that updates several records of one model at once, whilst simultaneously updating an associated model. It looks a bit like this: class Sport has_one :photo end class Photo belongs_to :sport acts_as_fleximage end class Page # the page is not related to either of the previous models end Just for ...

Association class in NHibernate

Hi, i want to map two classes m:n associated using NHibernate. NH would map a simple m:n association in a link table with foreign key constraints to the entity tables. Now I want to attach more attributes to the association as seen on this example: (and I want NHibernate to store these attributes in the link table) This UML diagram s...

ActiveRecord::HasManyThroughAssociationNotFoundError in UserController#welcome

I have a many to many relationship in rails. All database tables are named accordingly and appropriately. All model files are plural and use underscore to seperate words. All naming comventions are followed by ruby and rails standards. I'm using has many through in my models like this: has_many :users, :through => :users_posts #Post...

Can I access the id of the parent object inside a named_scope when fetching associated objects with the 'others' method?

Let's say you have two models: articles and comments. class Article < ActiveRecord::Base has_many :comments end You know you can fetch associated comments to an article like this: article = Article.first article.comments # => SELECT * FROM "comments" WHERE ("comments".article_id = 123) Is there a way to explicitly access the arti...

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

Model a person-to-person relationship in Ruby-on-Rails using has_many :through

I would like to model a person's relationship to another person, where the relationship isn't necessarily hierarchical (i.e. friends & colleagues, rather than parent & children) and I am interested in capturing more detail about each relationship (e.g. notes, type of relationship, date established). Finally, I would like to use the act_a...

Why does ActiveRecord not use the primary_key and foreign_key columns when generating the SQL for a join in an association?

I know I can use :finder_sql to manually define the SQL to use to fetch associated records, but I'm wondering if ActiveRecord uses the :primary_key and :foreign_key options on an association to generate the joining SQL. It doesn't appear to, but am I just missing something here? Update: To be more explicit, my question is: Is there s...

How can I delete child objects when the parent is deleted in rails?

model a: has_many :b, :dependent => :delete_all model b: belongs_to :a belongs_to :c model c: has_many :b When I delete an a, I would also like to have children b's deleted so that they get removed from any c's that may reference them. However, the above isn't working. I'd appreciate any help. ...

Which association should I use to describe `Types of things' in Ruby on Rails?

Hi, I'd like to be able to describe different types of a model using RoR associations. An example: Models: Post ImagePost post_id:integer url:string MessagePost post_id:integer message:string ImagePost and MessagePost are a type of Post. I'd like @posts = Post.all to retrieve both types of post and allow me access to their attri...

Does ActiveRecord save a belongs_to association when saving main object?

If I have two models: class Post < ActiveRecord::Base belongs_to :user end and class User < ActiveRecord::Base has_many :posts end If I do: post = Post.new user = User.new post.user = user post.save Does the user get saved as well and the primary key properly assigned in post's user_id field? ...

City belongsThroughCountyTo Province association, how to simplify the code?

Tables: Province hasMany County, County belongsTo Province, County hasMany City, City belongsTo County So basically something like: City belongsThroughCountyTo Province Situation: In a search form I have a select drop down menu with provinces. The "code": When I list the results, I first get ids of counties that belong to the speci...

Don't save an associatied object in Linq To Sql.

Hi. I have an entity (Address) that has a assosiated entity (Country). <Table Name="dbo.Address" Member="Address"> <Type Name="TS.Club.Domain.Model.ValueObject.Address"> <Column Name="Identifier" Member="Identifier" DbType="UniqueIdentifier NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="false" AutoSync="OnInsert" /> ...

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

has_many :through ,self referential association

I have trouble with the self referential association, the models should give ma an array of models for the left_chunks and right_chunks methods, but I get everytime an empty array The source class Chunk < ActiveRecord::Base has_many :left_bindings, :foreign_key => "left_chunk_id", :class_name => "ChunkChunk", :dependent => :des...