Has anybody had any luck having a has_one go through a has_many relationship in the same model. I keep getting
ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association
It seems like it would be easy take a set of has_many results and filter it down by a specific key and call it a has_one relationship.
Using rails 3.2.12
Here is my associations right now participation is a different model.
has_one :original_participation, :through => :participation
has_one :original_participant, :through => :original_participants, :foreign_key => "organization_id"
has_many :original_participants,
:through => :original_participation,
:source => :participants
I need to go through this last association and filter it down by organization_id.
ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association 'Surveys::Participant#original_participant' where the :through association 'Surveys::Participant#original_participants' is a collection. Specify a has_one or belongs_to association in the :through option instead.
With has_one you should not need :through There is no need for an intermediary relationship.
If I think I know what you're trying to do you have a hierarchy of tests:
has_one test_parent, :class_name => "Test", foreign_key: "child_test"
has_many tests
to call them:
#my_array_of_children = tests
#my_parents_id = test.id
etc.
Related
I'm trying to set up some new classes and associations in a Rails 2.2.2 app (please don't ask me to upgrade it).
The idea is that there is a Conversation class. It can have many participants, which are a mix of User and Pupil records, and it can have many owners, which can be a mix of a variety of things, including User and Pupil records. This is what I have so far:
#the :owner association can point to *anything*, including a Pupil or User record
class ConversationOwnership < ActiveRecord::Base
belongs_to :conversation
belongs_to :owner, :polymorphic => true
end
#the :participant association will point to either a Pupil or User record
class ConversationParticipant < ActiveRecord::Base
belongs_to :conversation
belongs_to :participant, :polymorphic => true
end
class Conversation < ActiveRecord::Base
has_many :conversation_ownerships
has_many :owners, :through => :conversation_ownerships
has_many :conversation_participants
has_many :participants, :through => :conversation_participants
end
This currently isn't working:
Conversation.first.participants
=> ActiveRecord::HasManyThroughAssociationPolymorphicError: Cannot have a has_many :through association 'Conversation#participants' on the polymorphic object 'Participant#participant'.
Now, I know that the has_many_polymorphs plugin was designed to solve this very problem, but the problem with using that is that it automatically makes associations for each of the listed classes, and because User and Pupil are in both participants and owners, they would clash:
OWNER_CLASSES = [:ilps, :lessons, :digilearning_modules, :resources, :pupil_groups, :pupils, :users]
PARTICIPANT_CLASSES = [:pupils, :users, :contacts, :parent_carers]
has_many_polymorphs :participants, :from => PARTICIPANT_CLASSES, :through => :conversation_participants, :order => "conversation_participants.created_at"
has_many_polymorphs :owners, :from => OWNER_CLASSES, :through => :conversation_ownerships, :order => "conversation_ownerships.owner_type, conversation_ownerships.created_at"
With this, the first hmp makes a .pupils association which effectively means "participants that are Pupils", and the second hmp also makes a .pupils association which effectively means "owners that are Pupils".
I don't want these extra associations that has_many_polymorphs brings, which is why i thought I would roll my own associations. But, I can't get past that Cannot have a has_many :through association 'Conversation#participants' on the polymorphic object 'Participant#participant'. error.
This feels like it should be possible - is it?
I have two models, AssetEvent and Subsystem, and a join model between them AssetEventSubsystem. They look like this:
RehabEvent < AssetEvent
has_many :asset_event_subsystems
has_many :subsystems, :through => :asset_event_subsystems
AssetEventSubsystem
belongs_to :rehab_event, :class_name => 'AssetEvent', :foreign_key => "asset_event_id"
belongs_to :subsystem
Subsystem
belongs_to :asset_type
When I try to run RehabEvent.new.asset_event_subsystems.build, I get back a ActiveRecord::UnknownAttributeError: unknown attribute: rehab_event_id, which is confusing, because I thought that should have been taken care of by the foreign_key option in the join model.
How do I make rails use the column in my database (asset_event_id) instead of the default?
Looks like u need that: ?
RehabEvent < AssetEvent
has_many :asset_event_subsystems
has_many :subsystems, :through => :asset_event_subsystems,
:foreign_key => "asset_event_id"
AssetEventSubsystem
belongs_to :rehab_event, :class_name => 'AssetEvent', :foreign_key => "asset_event_id"
belongs_to :subsystem
Subsystem
has_many :asset_event_subsystems
has_many :rehab_events, :through => :asset_event_subsystems
belongs_to :asset_type
And btw, i dont know your project, but can you use just has_and_belongs_to_many ?
You need to define the foreign_key on your has_many association as well as your belongs_to:
has_many :asset_event_subsystems, foreign_key: 'asset_event_id'
I'm having a problems with :group and :uniq tags as an option to has_many through collection definition. I have many triple model associations defined on my system and I want to list elements grouped to avoid (the actual) repetition. My main model looks like that:
class Trip < ActiveRecord::Base
belongs_to :agent
has_many :trips_destinations, :class_name => "TripsDestination"
has_many :destinations, :through => :trips_destinations
has_and_belongs_to_many :vibes, join_table: :trips_vibes
has_and_belongs_to_many :verbs, join_table: :trips_verbs
has_many :trips_destinations_activities, :class_name => "TripsDestinationsActivity"
has_many :activities, :through => :trips_destinations_activities, :uniq => true
has_many :trips_destinations_hotels, :class_name => "TripsDestinationsHotel"
has_many :hotels, :through => :trips_destinations_hotels
has_many :trips_destinations_recommended_places, :class_name => "TripsDestinationsRecommendedPlace"
has_many :recommended_places, :through => :trips_destinations_recommended_places
has_many :trips_destinations_transportations, :class_name => "TripsDestinationsTransportation"
has_many :transportations, :through => :trips_destinations_transportations
...
...
end
Anyone know how to list they why avoiding repetition given the pair [trip_id, destination_id]?
And why they are repeated?
My rails version is 4 and I the image below shows the error message when passing grouping options to has_many relation.
Please help!
Your syntax for uniq is not correct for Rails 4.
has_many :activities, -> { uniq }, :through => :trips_destinations_activities
Unique now has scope syntax.
I have a parent/child relationship via our users table, with models as such:
class User < ActiveRecord::Base
# Parents relationship
has_many :children_parents, :class_name => "ParentsChild", :foreign_key => "child_id", :dependent => :destroy
has_many :parents, :through => :children_parents
# Children relatiopnship
has_many :parents_children, :class_name => "ParentsChild", :foreign_key => "parent_id", :dependent => :destroy
has_many :children, :through => :parents_children
...
end
And in parents_child.rb:
class ParentsChild < ActiveRecord::Base
belongs_to :parent, :class_name => "User"
belongs_to :child, :class_name => "User"
end
Right now, it is possible in our "add children" form (just using vanilla nested attributes) to add the same user as a child multiple times for parents. I am not sure what the 'right' way to go about forcing uniqueness in the ParentsChild relationship, although I am leaning towards a unique index on (parent_id, child_id) at the database layer (using a migration of course).
I am sure I could also enforce uniqueness constraints in the UsersController::update method, but would prefer to avoid changing that code (right now it doesn't reference parents/children at all, thanks to nested attributes in the form/model) if possible. I am most concerned with making sure we use the "proper" solution. What is the 'right' or 'rails' way to do this?
Using has_many :through, you can specify :uniq as an option, like this:
has_many :parents, :through => :children_parents, :uniq => true
I'm struggling with a has_many association. I have a diary application. The model players are as follows:
User
UserFriend
UserFoodProfile
I want to be able to get at all the foods that a user's friends have eaten. So, I want to be able to get: current_user.friends.profiles
I've setup the associations properly so far so that I'm able to access current_user.friends, but now I want to be able to get all the friend's entries as well over the last 30 days.
Here are my models
class User < ActiveRecord::Base
cattr_reader :per_page
##per_page = 20
has_many :user_food_profiles
has_many :preferred_profiles
has_many :food_profiles, :through => :user_food_profiles
has_many :weight_entries
has_one :notification
has_many :user_friends
has_many :friendships, :class_name => "UserFriend", :foreign_key => "friend_id"
has_many :friends, :through => :user_friends
class UserFriend < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
class UserFoodProfile < ActiveRecord::Base
belongs_to :user
belongs_to :food_profile
belongs_to :post
The UserFriend model is setup the following way:
id
user_id
friend_id
friend_name
I want to connect to user_food_profiles from friend so that I can get a user's friend's current user_food_profiles as "entries" but everything I've tried hasn't worked. How would I setup this association?
Tried to do:
UserFriend: has_many :user_food_profiles, :as => 'entries'
UserFoodProfile: belongs_to :friend, :foreign_key => 'friend_id'
Any ideas on how to make this work? Tempted to create a custom finder_sql but I'm sure this can work with associations.
Isn't a "friend" just another user that's in the database?
Let your UserFriend be a many_to_many relationship (either with "has_and_belongs_to_many" or "has_many :through"): each user can have several users as friends.
You can then link those user_ids (which could be in the many_to_many table called 'friend_id' if you like) to their foodprofile without a problem, since it is using the same link as user.foodprofile .
This is the line I see being the problem:
class User < ActiveRecord::Base
# <snip/>
has_many :friendships,
:class_name => "UserFriend",
:foreign_key => "friend_id"
I'm assuming that you're using a join table here called user_friend. That would mean that the foreign key there should be "user_id".
Now, unless you're going to store extra metadata in that UserFriend model, it's not required — you can get away with a self-referential has_and_belongs_to_many relationship like so:
has_and_belongs_to_many :friends,
:class_name => "User",
:join_table => "user_friends",
:foreign_key => "user_id",
:association_foreign_key => "friend_id"
Doing this, all you have to do is user.friends.profiles quite easily.
Now, if the relationship is bi-directional it gets a bit more complex, but I feel like this should at least get you started along the way.