Nested Through Has Many Associations - ruby-on-rails

I am having some trouble creating a nested association and I can't quite spot what's going on:
class Package < ActiveRecord::Base
has_many :selections, :class_name => "PackageSelection"
has_many :channels, :through => :selections
has_many :categories, :through => :channels, :source => ?????
end
class Channel < ActiveRecord::Base
belongs_to :category
has_many :package_selections
has_many :packages, :through => :package_selections
end
class Category < ActiveRecord::Base
has_many :channels
end
I am trying to figure out how to build the association to categories. Any advice?

It seems like this is impossible in Rails Core (currently 2.3) but someone has written a plugin to allow it

Related

How to state a "direct" has_many association between two models through a third model?

I am using Ruby on Rails 4.1 and I would like to implement a "direct" has_many association between Article and CommentRequest in the following case:
class Article < ActiveRecord::Base
has_many :comment_associations
has_many :comments, :through => :comment_associations
end
class Comment < ActiveRecord::Base
has_many :article_associations
has_many :articles, :through => :article_associations
has_many :comment_request_associations
has_many :comment_requests, :through => :comment_request_associations
end
class CommentRequest < ActiveRecord::Base
has_many :comment_associations
has_many :comments, :through => :comment_associations
end
That is, I would like to state a has_many association in both Article and CommentRequest in order to make it possible to run code as-like the following:
#article.comment_requests
#comment_request.articles
If it is possible, how to make that?
You should be able to do
class Article < ActiveRecord::Base
has_many :comment_associations
has_many :comments, :through => :comment_associations
has_many :comment_requests, :through => :comments
end
since rails allows you to nest has_many :through relationships
and similarly
class CommentRequest < ActiveRecord::Base
has_many :comment_associations
has_many :comments, :through => :comment_associations
has_many :articles, :through => :comments
end

How do I collect all the topics that are owned by the members of the groups I am a member of?

This may be tough for me to explain, so if it's not clear just let me know so I can edit as needed!
I have the following example:
class User < ActiveRecord::Base
has_many :topics
has_many :memberships
end
class Topic < ActiveRecord::Base
belongs_to :user
end
#join model between User and Group
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships, :source => :user
has_many :topics, :through => :members
end
The problem I'm having is that I am trying to create a feed (#feed_topics) of all topics that are owned by all the members of the groups I am a member of, and I'm driving myself a little nuts.
Should I try to make this happen using associations, or make an instance method in my User model that has some ActiveRecord/SQL to union all the groups' members' topics into one ActiveRecord::Relation object?
My goal is to write current_user.feed_topics in my controller's action.
Sorry for not explaining earlier! The idea was to utilize 'Nested has_many_through's in order to get to your feed topics. This concept is documented here under the heading 'Nested Associations': http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html.
Let me know if this still is unclear (or if it doesn't work).
class User < ActiveRecord::Base
has_many :topics
has_many :memberships
has_many :groups, :through => :membership
has_many :group_members, :through => :groups, :source => :member
has_many :feed_topics, :through => :group_members, :source => :topic
end
So far these are the final versions of the models from the original question (topic and membership did not change):
class User < ActiveRecord::Base
has_many :topics
has_many :memberships
has_many :groups, :through => :memberships
has_many :feed_topics, :through => :groups, :source => :member_topics
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships, :source => :user
has_many :member_topics, :through => :members, :source => :topics
end
I am testing right now by adding more groups and members to see if it pulls in all the other members' topics of other groups.
EDIT: things seem to be working ok.
EDIT2: one little problem I had was seeing duplicate topics because a member was in multiple groups. I learned about :uniq => true and it saved the day.

Trouble with has_many :through => association

Caution: I am a 4 week old at programming. I am having trouble with a has_many :through => relationship between my Neighborhood and Cta_train models.
Here are my models:
class CtaTrain < ActiveRecord::Base
belongs_to :Ctaline
has_and_belongs_to_many :searches
has_many :neighborhoods, :through => :CtaLocation, :foreign_key => :neighborhood_id
has_many :CtaLocations
end
class Neighborhood < ActiveRecord::Base
has_many :geopoints
has_many :listings
has_many :properties
has_and_belongs_to_many :searches
has_many :CtaTrains, :through => :CtaLocation, :foreign_key => :cta_train_id
has_many :CtaLocations
end
class CtaLocation < ActiveRecord::Base
belongs_to :neighborhood
belongs_to :CtaTrain
end
When I try to do this:
neighborhood.CtaTrains
I get this error:
ActiveRecord::HasManyThroughAssociationNotFoundError (Could not find the association :CtaLocation in model Neighborhood):
I have been slogging through this for several hours now....I have tried many iterations of ideas from stackoverflow....what I show above feels like the closest solution, but obviously still not working. Any thoughts would be appreciated!
I think the problem is that you're not following Rails conventions by using lowercase/underscore for your symbols. Class names have to be CamelCase but you should be doing the following everywhere else:
class CtaTrain < ActiveRecord::Base
belongs_to :cta_line
has_and_belongs_to_many :searches
has_many :neighborhoods, :through => :cta_locations, :foreign_key => :neighborhood_id
has_many :cta_locations
end
*Update: You should also be using :cta_locations (plural) in your has many through

:has_many relationship with :through on another relationship with :through

My setup is as follows:
class User < ActiveRecord::Base
has_many :owners, :dependent => :destroy
has_many :properties, :through => :owners
end
class Owner < ActiveRecord::Base
belongs_to :user
belongs_to :property
end
class Property < ActiveRecord::Base
has_many :owners, :dependent => :destroy
has_many :users, :through => :owners
has_many :datafiles, :dependent => :destroy
end
class Datafile < ActiveRecord::Base
belongs_to :property
end
Now I'd like to be able to do #user.datafiles.
I tried has_many :datafiles, :through => :properties, :source => :datafiles but there appears to be a problem with a :through on something that's already went to a :through. So how would I go about to try and manage what I'm trying to do here?
Thank you in advance.
2 approaches;
1>
class User < AR
has_many :owners, :dependent => :destroy
has_many :properties, :through => :owners
has_many datafiles
end
class Datafile < AR
belongs_to :user
belongs_to :property
end
Your requirement of user.datafiles should be fulfilled with this.
If you want a nested has_many through, you'll need to use a plugin which is the 2nd approach.
2>
You can find it here.
The plugin works out of the box and does the job.
How about something like:
#user.rb
def datafiles
Property.find(:all, :joins => :owners, :conditions => ['owners.user_id = self.id'], :include => :datafile).collect(&:datafile)

rails, accepts_nested and has_many :through is creating duplicate entries

I have a store model that has many products with a has_many :through relationship.
I have this working with accepts_nested_attributes, but the result is that rails is making duplicate associates.
I don't have anything special going on it is a very simple app.
Any ideas on why duplicates associates are getting created?
look at answer : how to avoid duplicates in a has_many :through relationship? here :
add :uniq => true to the has_many :through
class Blog < ActiveRecord::Base
has_many :blogs_readers, :dependent => :destroy
has_many :readers, :through => :blogs_readers, :uniq => true
end
class Reader < ActiveRecord::Base
has_many :blogs_readers, :dependent => :destroy
has_many :blogs, :through => :blogs_readers, :uniq => true
end
class BlogsReaders < ActiveRecord::Base
belongs_to :blog
belongs_to :reader
end
This is a confirmed bug in Rails, with a fix set to be included in 2.3.6.
https://rails.lighthouseapp.com/projects/8994/tickets/3575-multiple-join-records-when-using-nested_attributes-in-habtm

Resources