associations

CakePHP 1.3.4 $belongsTo problem incase of Relationship table

I have a table called user_relationship. which has two foreign keys refering back to the User table to map that they are friends. CREATE TABLE `user_relationships` ( `id` int(11) unsigned NOT NULL auto_increment, `status` varchar(255) default 'pending', `time` datetime default NULL, `user_id` int(11) unsigned NOT NULL, `frien...

In cakephp, how can I set conditions for a model that is not associated with the one I am paginating but which is associated to one that is?

I am paginating ($this->Customer->paginate()) on the 'Customer' model. The customer model is associated to the 'Contact' model which in turn is associated to the 'ContactAddress' model. So: Customer hasMany Contact Contact belongsTo ContactAddress Now I want to paginate customers in the 'Customers->index()' using a search query let'...

Rails - Best-Practice: How to create dependent has_one relations

Could you tell me whats the best practice to create has_one relations? f.e. if i have a user model, and it must have a profile... How could i accomplish that? One solution would be: # user.rb class User << ActiveRecord::Base after_create :set_default_association def set_default_association self.create_profile end end Bu...

How do I use searchlogic to search on a belongs_to association?

Contact belongs_to status_contacts I only want those Contacts where no value has been assigned. I installed the searchlogic plugin. I tried: contacts = Contact.status_contact_null And got an error. How can I get a full sense of how to use associations with searchlogic, and how can I use it for this particular search? ...

New model object through an association

I thought it was possible to create a new model object through an association? class Order < ActiveRecord::Base belongs_to :basket end class Basket < ActiveRecord::Base has_one :order end order = Order.new() basket = order.basket.new() # NoMethodError: undefined method `new' for nil:NilClass ...

Rails 3 - Getting NameError after accessing models via has_many

Hey folks, I have following models: class App::Module::Object < AR::Base has_many :objects, :class_name => 'App::OtherModule::Object' end and class App::OtherModule::Object < AR::Base belongs_to :object, :class_name => 'App::Module::Object' end Using Rails 3.0.0 and Ruby 1.9.2, I get following error after accessing App::Module...

Do has_many :through associations work for models that only exist in memory?

for class A < ActiveRecord::Base has_many :bs has_many :cs, :through => :bs end class B < ActiveRecord::Base belongs_to :a belongs_to :c end class C < ActiveRecord::Base has_many :bs end If i bring up a rails console, and do a = A.new b = a.bs.build b.c = C.new Then i get a.cs => [] but a.bs[0].c => c If a is save...

Having trouble updating an attribute with an association attached to it

I have two models here: Package and Status Package belongs_to status Status has_many packages So, my Package model has a status_id column In my Packages controller, I've got this method (that receives data from an ajax POST call): def edit_status @status = Status.find_by_name(params[:status]) Package.update(params[:id], :status_...

Find set that belongs_to another set

I'm sure there's an easy answer for this, but not sure how to word it to search for it. Given that articles belong to users, if I have a set of three different users, how can I access all of the articles written by any of those users with one query? ...

Change order of elements in Rails association collection

If I have a collection of objects through a 'has and belongs to many' association in Rails (e.g. the album 'summer photos' has a collection of photos), how can I arbitrarily change the order of the elements in that collection? For example, there is a default index that would yield @album.images[0] or .first. how would I go about ch...

Entity Framework inheritance association problem

Hi everyone, I am trying to resolve a situation I ran into when implementing EF with my project. I have absolved the table-per-type approach, in my case the ActionUpdate derives from the ActionHistory and that works fine. What I am trying to achieve is to derive the ActionUpdate from the ActionHistory, and to have a navigation propert...

Sorting rows by count of a many-to-many associated record

I know there are a lot of other SO entries that seem like this one, but I haven't found one that actually answers my question so hopefully one of you can either answer it or point me to another SO question that is related. Basically, I have the following query that returns Venues that have any CheckIns that contain the searched Keyword ...

Setting a :has_many :through association on a belongs_to association Ruby on Rails

I have three models each having the following associations class Model1 < ActiveRecord::Base has_many :model2s has_many :model3s end class Model2 < ActiveRecord::Base belongs_to :model1 has_many :model3s, :through => :model1 #will this work... is there any way around this end class Model3 < ActiveRecord::Base belongs_to :model1 has_m...

Containable fails to JOIN in belongsTo relationships when 'fields' are used in CakePHP 1.3.4

(CakePHP Version 1.3.4) I have the following association between a Contact model with Account and Test models: class Contact extends AppModel { var $name = 'Contact'; var $actsAs = array('Containable'); var $hasMany = array( 'Test' => array( 'className' => 'Test', 'foreignKey' => 'contact_i...

@object.attributes = params[:object] is saving HABTM associations, how avoid it?

Sorry for my english ... I need set some attributes to my object, but, I don't want to save them, I will save them after. @object.attributes = params[:object] @object.save is working with other attributes like name, description ... but when I send habtm_object_ids[] rails save the association at this point: @object.attributes = para...

How can I count and filter on an associated record in Rails?

Company has_many Contacts Company has_many ContactEmails :through => :contacts ContactEmail belongs_to Contact Contact belongs_to Company Contact has_many ContactEmails For a specific instance of Company, how can I find all ContactEmails and filter on an attribute, such as date_sent? I tried Company.ContactEmails but I don't think ...

Associating users with nested models in rails

I just added users to my app using Authlogic and CanCan. I just have a quick question about best practices for associating users to my other models. I have 3 models that are nested within each other with one-to-many associations like this: SENTENCE > WORD > LETTER So a sentence has many words, and a word has many letters. I am just ...

Rails ActiveRecord association "one or the other"

I was wondering, how an association like the following might be done in Rails: class Car < ActiveRecord::Base belongs_to :person end class Truck < ActiveRecord::Base belongs_to :person end class Person < ActiveRecord::Base #How to do the association as below? has_one :car or :truck end Essentially, I am trying to enf...

Creating third level associations where second level belongs_to third on Rails ActiveRecord

Hello all, I have the following model, Purchase, on my Rails app: class Purchase < ActiveRecord::Base [...] belongs_to :payment, :validate => true belongs_to :day, :foreign_key => :day_day, :primary_key => :day, :counter_cache => true [...] end And I have the Day model: class Day < ActiveRecord::Base [...] has_many :...

Rails won't save my foreign key to the database

So basically I have two models, Entry and Comment. I have the association setup so that Entry has many Comments: class Entry < ActiveRecord::Base has_many :comments end And a comment belongs to an entry: class Comment < ActiveRecord::Base belongs_to :entry end In my database schema I have setup the comments table with a column call...