Run.rb:
has_many :schedule_machines, through: :schedule_locations
Schedule.rb:
has_many :schedule_locations, dependent: :destroy
has_many :schedule_machines, through: :schedule_locations
has_many :assigned_schedule_machines, through: :runs, source: :schedule_machines
has_many :runs, dependent: :destroy
when I look at a Schedule in the console,
schedule.assigned_schedule_machines
gives an undefined method.
How come?
has_many :runs, dependent: :destroy
has_many :assigned_schedule_machines, through: :runs, source: :schedule_machines
You need to order the "runs" first. The assigned_schedule wants to use runs; but in your original code the runs wasn't defined yet.
Related
I've got a huge model with a lot dependencies.
has_many :names, dependent: :destroy
has_many :links, dependent: :destroy
has_many :fruits, dependent: :destroy
has_many :images, dependent: :destroy
has_many :houses, dependent: :destroy
has_many :users, dependent: :destroy
has_many :cars, dependent: :destroy
has_many :places, dependent: :destroy
has_many :statistics, dependent: :destroy
Is there a possibility to wrap it with a "depend: destroy"-block for more clarity?
This is just an example. In my model are 34 connected models.
I have been experiencing problems when trying to destroy_all my users in my database. When I run User.destroy_all I get:
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'Account#hotel_partnerships' because the source reflection class 'Partnership' is associated to 'Hotel' via :has_many.
I suspect that the main problem is that in my schema I have an accountmodel that belongs_to a user(the owner of the account) and at the same time the account has_many users through its offices and hotels.
Here's how my associations look like :
- Account.rb
class Account < ActiveRecord::Base
# associations
has_many :hotel_partnerships, through: :hotels, source: :partnerships, dependent: :destroy
has_many :office_partnerships, through: :offices, source: :partnerships, dependent: :destroy
has_many :hotel_users, through: :hotels,source: :users, dependent: :destroy
has_many :office_users, through: :offices, source: :users, dependent: :destroy
has_many :hotels, dependent: :destroy
has_many :offices, dependent: :destroy
has_many :rooms, through: :hotels, dependent: :destroy
has_many :tasks, through: :hotels, dependent: :destroy
belongs_to :admin, class_name: "User", foreign_key: "admin_user_id", dependent: :destroy
- User.rb
class User < ActiveRecord::Base
## this line adds auth token to all user on creation
acts_as_token_authenticatable
# associations
belongs_to :account, dependent: :destroy
has_and_belongs_to_many :hotels
has_and_belongs_to_many :offices
has_many :tasks, dependent: :destroy
has_many :bookings, dependent: :destroy
has_one :created_account, class_name: "Account", foreign_key: "admin_user_id", inverse_of: :admin, dependent: :destroy
- Partnership.rb
class Partnership < ActiveRecord::Base
belongs_to :hotel
belongs_to :office
- Hotel.rb
class Hotel < ActiveRecord::Base
geocoded_by :full_address
after_validation :geocode
has_many :partnerships
belongs_to :account
has_and_belongs_to_many :users
has_many :tasks, dependent: :destroy
has_many :rooms, dependent: :destroy
has_many :beds, through: :rooms, dependent: :destroy
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos
accepts_nested_attributes_for :rooms
Is it possible to fix this circular association problem ?
A character has_many :conversations, through: :chats.
However, each conversation belongs to two or more characters: Each conversation_id in the chats table is repeated at least twice, each with a different character_id, pointing to multiple characters of the same conversation.
character.rb
has_many :chats, foreign_key: "character_id", dependent: :destroy
has_many :conversations, through: :chats, source: :conversation
chat.rb
belongs_to :character
belongs_to :conversation
conversation.rb
has_many :messages
Is there a nice quick railsy way to list all the characters that a conversation belongs to? You'd think something like #conversation.characters would do the trick, but rails doesn't recognise this.
You have to set up the relationship from your Conversation model as well.
has_many :chats, dependent: :destroy
has_many :characters, through: :chats
Then you can easily call #conversation.characters
Quick tip
By the way, your Character model can be simplified, no need to define the standard foreign_key and source.
has_many :chats, dependent: :destroy
has_many :conversations, through: :chats
I have three models: Almon, Bela and Lingo
class Almon < ActiveRecord::Base
belongs_to :bela, dependent: :destroy
has_many :lingos, through: :bela, dependent: :delete_all
accepts_nested_attributes_for :bela
accepts_nested_attributes_for :lingos, :allow_destroy => true
end
I am trying to submit a nested form in Almon and I am getting
Cannot modify association 'Almon#Lingos' because the source reflection class 'Lingo' is associated to 'Bela' via :has_many.
Could anyone please tell me what I am doing wrong?
You need to use has_many instead of belongs_to as the following:
has_many :belaes, dependent: :destroy
has_many :lingoes, through: :belaes, dependent: :delete_all
I'm not sure what has changed getting this error when trying to login:
ERROR: relation "tags" does not exist
LINE 1: SELECT DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" O...
The tag model:
class Tag < ActiveRecord::Base
attr_accessor :unread_count, :user_feeds
has_many :taggings
has_many :feeds, through: :taggings
end
The tagging model:
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :feed
belongs_to :user
end
And the user relationships:
class User < ActiveRecord::Base
has_one :coupon
has_many :subscriptions, dependent: :delete_all
has_many :feeds, through: :subscriptions
has_many :entries, through: :feeds
has_many :imports, dependent: :destroy
has_many :billing_events, as: :billable, dependent: :delete_all
has_many :taggings, dependent: :delete_all
has_many :tags, through: :taggings
has_many :sharing_services, dependent: :delete_all
has_many :unread_entries, dependent: :delete_all
has_many :starred_entries, dependent: :delete_all
has_many :saved_searches, dependent: :delete_all
has_many :actions, dependent: :destroy
belongs_to :plan
end
This error is new and came right after I tried to install the intercom.io gem. I removed the gem, did a gem cleanup, reset the db and still the same error.
Not sure what happened, if anyone has any suggestions I would appreciate it.
Checked missing tables, and that was the problem.