associations

Creating a created_by column and association with rails?

Sigh... I feel like a big newbie on this one, so lets say I have a few models: class Question < ActiveRecord::Base has_many :answers belongs_to :user end class Answer < ActiveRecord::Base belongs_to :question has_one :user end class User < ActiveRecord::Base has_many :questions has_many :answers, :through => :questions end...

Rails idiom to avoid duplicates in has_many :through

I have a standard many-to-many relationship between users and roles in my Rails app: class User < ActiveRecord::Base has_many :user_roles has_many :roles, :through => :user_roles end I want to make sure that a user can only be assigned any role once. Any attempt to insert a duplicate should ignore the request, not throw an error ...

VS .NET class designer

Assume that class Being has a property of type Habitat. Both classes are implemented in their own .cs files. When I drag and drop the two classes from the class designer onto a new Class diagram, I do not see an association line from Being to Habitat, whereas there is an association in code. Am I doing something wrong, or this simply isn...

belongs_to association query method doesn't exist

The "association?" query method that the Rails docs say should exist when I create a belongs_to association doesn't actually get created: class Author < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :author end >> p = Post.create(:author => Author.create) >> p.author? NoMethodError: undefined me...

Accessing Associations in a View with CakePHP

Sorry if this question is too vague, but I'd rather not muddy it's point with my assumptions as to what may or may not actually be relevant background information. If I create an association such as Employee belongsTo Company When I create a view for Employee and want to display their Company name how can I simply display the company n...

Inheritance and Associations in NHibernate

I have an entity model where the base class in an inheritance structure has an association with another class, and was wondering if the subtypes of the base class will have the association mapped up as well? For a bit more information, here is a basic outline of this part of the system: Transport is the base class, and has an associati...

ActiveRecorded associated model back reference

Hello, It is easy to associate a model to another using has_many/belongs_to methods. Let's suppose the following models: class Movie < ActiveRecord::Base has_many :actors end So, I can find the actors from a given movie instance. But now, given an actor instance obtained through the actors association, I'd like to find the movie ...

Rails Has One relation

I have a simple database that has the following relation. Each Server has one Game. now the table Game is just a list of the different games that are possible to have on the server. However, when I do a @server.game, I get a SQL exception because it's trying to find the server's INSTANCE of it's game (Select * from games where games.s...

Rails form with three models and namespace

Banging my head against this one for a long time. On Rails 2.3.2, Ruby 1.9.1. Trying to use one form to create three objects that have these relations: class Person has_one :goat end class Goat belongs_to :person has_many :kids end class Goat::Kid belongs_to :goat end Here's a summary of the schema: Person first_name l...

Named Scope Problem With "Has Many Through" Association

I'm using named scopes to process some filtering actions, and the log is showing that everything is working perfectly, except that after the app goes and finds the correct data, it then ignores what it found and just lists a find.all instead of the filtered result. Here's the details. I have 3 models: Users, Markets and Schedules. U...

has_many :through issue with new records

I'm modeling "featuring" based on my plan in this question and have hit a bit of a stumbling block. I'm defining a Song's primary and featured artists like this: has_many :primary_artists, :through => :performances, :source => :artist, :conditions => "performances.role = 'primary'" has_many :featured_artists, :through =...

How to create has_and_belongs_to_many associations in Factory girl

Given the following class User < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base has_and_belongs_to_many :users end how do you define factories for companies and users including the bidirectional association? Here's my attempt Factory.define :company do |f| f.users{ |users| [users.a...

Want to sort by association records count in Datamapper

Lets say I have the following DataMapper resources: class Post include DataMapper::Resource has n, :comments ... end class Comment include DataMapper::Resource belongs_to :post ... end To get the ordered list of posts, I know you can do: @posts = Posts.all(:order => :date.desc) But lets say I want to display al...

NHibernate - Mapping parent/child one-to-many association from same table

I'm pretty new to NHibernate and am having a problem getting this kind of mapping to work. I'm using NHibernate 2.1.0.GA and NHibernate.Mapping.Attributes 2.0. I have a single table (t_Posts) related to itself as a parent/child relationship: t_Posts ------------------------ (PK) PostID bigint DatePosted datetime Body nvarcha...

How to do HABTM management with auto completion in Rails?

I am looking for a good solution for a probably typical problem of managing models with HABTM association in Rails. Let's assume that we have two models -- products and categories: Products has_many :categorizations has_many :categories, :through => :categorizations Categories has_many :categorizations has_many :products, :thro...

How can I create new records with has_many :through and honor :conditions?

Let's say I have a Course in which Students can enroll via a Membership (e.g. a has_and_belongs_to_many relationsip of Courses and Students). Some memberships are for students who are just observing the class (not for credit, etc.), so: class Course < ActiveRecord::Base has_many :memberships has_many :students, :through...

Ruby on Rails User Setup

Currently, I have a 6-model ruby on rails application, that I added authlogic to. The overall setup is User :has_many categories, topics,messages Categories has_many topics, Topics has_many messages (With and the corresponding opposite belongs_to links). When I try to access current_user.categories.find(2), no results are returned i...

Mentors for Architects

While reading the activities performed by established solution/software architects, i came to know that many of them are guided by mentor or mentors. So, Did you become an architect by the help of a mentor? What are the activities / tips / methodologies etc etc you learned from your mentor? How you got linked with your mentor? What...

Pull objects from an association into an array in Rails

A user has_many :donations, a project has_many :donations, and a donation belongs_to :user and belongs_to :project. I'm looking for a sensible way to extract the projects associated with a user (through donations) into an array. I'm currently doing: def index @user = User.find params[:user_id] @projects = [] @user.donations.each...

Update in rails for has_many associations

I have a has_many association, and a UI that uses javascript to dynamically add/remove as many has_many items as the user wants. After they are done, they hit save, which would call update on the parent. When I do this, the child items do not have any IDs, and the controller does not know how to handle it, so it just ignores it. How can ...