associations

Error when creating associated objects in Rails

I've got a create action that is attempting to create a Rating and a Programme in one go: def create @rating = current_user.ratings.create(params[:rating]) @rating.create_programme(params[:programme]) redirect_to ratings_path end In this code, a rating belongs to both a user and a programme and a user has_many :rat...

Add shortcut methods or follow ActiveRecord semantics?

Yet another argument with a friend of mine. Consider this code: class User < ActiveRecord::Base has_many :groups def in_group?(group) groups.include?(group) end end class Group < ActiveRecord::Base has_many :members def add_user(user) members << user end end My opinion is that these methods add extra unnecessar...

Registering an application to open specific web links

My question is the following: When using software such as Skype, TVU player or several other applications, it is possible to use weblinks specifically formatted for this application to launch it with certain parameters. For TVU for example a link on a web page could be: tvu://some-channel-id Clicking on this launches the application, ...

Creating a form for a new associated object using the Rails 2.3 model nesting.

Trying to use the nested model form in the new rails release candidate. I have this, but it only renders form fields for each existing associated photo object. (Given a form builder f that was created for my parent model object) %h3 Photos - f.fields_for :photos do |photo_form| %p = photo_form.label :caption = photo_form.te...

Linq To SQL Without Explicit Foreign Key Relationships

I am working with a few legacy tables that have relationships, but those relationships haven't been explicitly set as primary/foreign keys. I created a .dbml file using "Linq To Sql Classes" and established the proper Case.CaseID = CaseInfo.CaseID association. My resulting class is CasesDataContext. My Tables (One to many): Case ---...

Return related objects in select linq to sql

I have 2 tables Account and Address which has 1:1 relationsip; account has addressid in it. I have created the association in the dbml file. Now I want to write a query to select the account for given accountid and the resulting account should contain the address object in it as well. using (var context = new SalesLogixDataClassesData...

Triple join in Ruby on Rails

Hello everyone I have question regarding associations in Ruby on Rails. In the application there are projects, users, roles and groups. The project belongs to a group with users, a user can belong to many different groups but can only have one specific role within that group. For example: In one group the user is the project owner, but...

What is causing this ActiveRecord::ReadOnlyRecord error?

This follows this prior question, which was answered. I actually discovered I could remove a join from that query, so now the working query is start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true] This appears to work. However, when I try to move th...

Find the associations for an ActiveRecord class at run-time?

I would like to find the assocations of an ActiveRecord class at runtime... Let's assume I have the following: class Person < ActiveRecord::Base has_many :chairs has_many :pens end class Chair < ActiveRecord::Base belongs_to :person end class Pen < ActiveRecord::Base belongs_to :person end How can I find out at runtime that...

How to relate multiple models to one model that will rule them all, in Rails?

Let's say I have four completely independent models (Movie, Book, Game, Album) that control the types of things I have in my media collection. With them I can CRUD and tag individual albums, movies etc. But I need to keep track, and do some stuff that is common to, all of them. So I figured I need an Item model that would give me a tabl...

has_many :through association

Is it possible to make some kinda this association: User model: has_many :goods, :through => :assignments has_many :goods_want, :through => :assignments, :source => :good, :conditions => "assignments.type = 1" Testing in console u = User.first u.goods_want << Good.first u.save This is being logged: INSERT INTO `assignments` (`goo...

Polymorphic Associations in Rails

How do polymorphic associations work in Rails? What are their advantages? Is there a way to add belongs_to method just by running a migration? ...

Polymorphic Association doesn't save ClassName (without STI), any hints??

Hi, I have two models, one called Notes and one called Comments. Comments can be associated to many other models so I use a polymorphic association. In the schema.rb it looks like this: create_table "comments", :force => true do |t| t.text "body" t.integer "user_id" t.integer "commentable_id" t.integer "commentable_type" t.dat...

Nested Object w/ Checkboxes - mass-assignment even with accepts_nested_attributes_for ?

I thought that there should have been a simple solution to this, given that Rails 2.3 has this newfangled nested forms feature. Basically I want to create or update a user and assign them roles at the same time. It seems like I'm doing everything right but I get the error WARNING: Can't mass-assign these protected attributes: roles_attr...

What would be the best way to design database and relationships for a website like Yelp or eBay (lots of categories and sub-categories)?

Do they have a table for all categories and another for all sub-categories (and another for the sub-sub-categories and so on), or what? How do all the levels go around communicating with each other? I'm a noob getting started on a project that might have that level of complexity and I am having a hard-time wrapping my head around that. ...

Rails associations with the same models

I have two classes with the following associations: class Incident has_one :assignee has_one :technician class User has_many :incidents Note that the assignee and technician fields refer to objects of type User. How should these relationships be in the model? ...

does cloning affect activerecord callbacks?

I have a sequence of ActiveRecord objects that I want to cascade destroy but some of the objects are not being removed. Essentially I have as follows:- class Project < ActiveRecord::Base has_many :tasks, :dependent => :destroy has_many :schedules, :dependent => :destroy has_many :project_schedules, :through => :schedules, :class...

Updating eager-loaded associations in memory

In my current rails app I'm pulling a lot of data from the database into memory so that I can start a long-running background task that I don't want to hit the database every 5 seconds. This is all working great but I have some inelegant code that I'd like to get rid of if possible. For an example, I have the situation where I have a U...

Why would association.build not assign parent_id to it child?

I've got the following models: In plan.rb has_many :tickets and in ticket.rb belongs_to :plan validates_presence_of :plan_id When executing the following code in the controller @plan.tickets.build( ... ) @plan.save save fails with error_message for ticket: plan can't be blank. (plan is valid.) ...

Find all objects with no associated has_many objects

In my online store, an order is ready to ship if it in the "authorized" state and doesn't already have any associated shipments. Right now I'm doing this: class Order < ActiveRecord::Base has_many :shipments, :dependent => :destroy def self.ready_to_ship unshipped_orders = Array.new Order.all(:conditions => 'state = "...