Rails: how to join two models through two different relations? - mysql

I have two models: Saft (a magazine) and Keyword. Each "Saft" is defined by a series of keywords, but also has a title, which is always one of its keywords. The Saft and Keyword models are connected through a HABTM join table in order to pull all the keywords and now I am trying to pull the title from the keywords table onto the saft/show.html.erb, too. I am trying to use the class_name option in order to pull the title. Therefore I created the Edition model.
class Saft < ActiveRecord::Base
# attr_accessible :colour, :cover_alt, :description, :number, :short
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
has_one :title, :through => :edition, :class_name => "keyword"
has_one :edition
end
class Keyword < ActiveRecord::Base
# attr_accessible :word, :description
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
belongs_to :issue, :through => :edition, :class_name => "saft"
belongs_to :edition
end
class Edition < ActiveRecord::Base
# attr_accessible :saft_id, :keyword_id
belongs_to :title
belongs_to :issue
end
class SaftsController < ApplicationController
def show
#saft = Saft.find(params[:id])
end
show.html.erb
<%= #saft.title.upcase %>
I get the following error:
Started GET "/safts/2" for 127.0.0.1 at Sat Feb 10 17:31:28 +0100 2018
Connecting to database specified by database.yml
Processing by SaftsController#show as HTML
Parameters: {"id"=>"2"}
Saft Load (1.8ms) SELECT `safts`.* FROM `safts` WHERE `safts`.`id` = ? LIMIT 1 [["id", "2"]]
Image Load (0.3ms) SELECT `images`.* FROM `images` WHERE `images`.`saft_id` = 2
Rendered safts/show.html.erb within layouts/public (35.0ms)
Completed 500 Internal Server Error in 103ms
ActionView::Template::Error (uninitialized constant Saft::keyword):
29: </div>
30: <div class="saft_box col-content">
31: <div class="saft_keyword">
32: <strong><%= #saft.title.upcase %></strong>
33: </div>
34: <div class="saft_description">
35: <p><%= #saft.description %></p>
app/views/safts/show.html.erb:32:in `_app_views_safts_show_html_erb___758994895_2167416580'
How can I get this working?
When uncommenting #saft.title.upcase this implementation also breaks the Saft Keyword association, which I get working again by uncommenting the belongs_to :issue section in the Keyword model.

Some changes in your models:
class Saft < ActiveRecord::Base
#You don't need attr_accessible for fields in safts table
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
#The specific keyword that acts as title.
#You need a new field in safts table named title_id which references to a Keyword.
belongs_to :title, class_name => "Keyword", :foreign_key => 'title_id'
end
class Keyword < ActiveRecord::Base
#You don't need attr_accessible for fields in keywords table
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
end
To get the Saft title, you use #saft.title.word.upcase if #saft.title
I don't think you need anything else on your models for the use case in your OP.

Related

Many to many join table not updated in Active admin

I have two models Destination and Package. A package may have multiple Destinations and Destination may contain multiple Packages. In order to maintain the relationship, I have a table called packages_in_destinations. While I tried to insert destination from package form in active admin, it doesn't show up any errors but the packages_in_destinations table is not updated. I am using MySQL for the database.
# app/admin/package.rb
ActiveAdmin.register Package do
permit_params :package_id, :package_title, :description, :featured_image, :duration, :meta_description, :meta_keywords, :published, :package_categories_id, :destinations_id
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Package Details" do
f.input :destination_id, :as => :check_boxes, :collection => Destination.all.collect {|destination| [destination.destination_title, destination.id]}
end
f.actions
end
end
# app/models/package.rb file
class Package < ApplicationRecord
validates_uniqueness_of :package_title, scope: :package_title
validates :package_title, :description, :package_categories_id, :presence => true
belongs_to :package_category
has_and_belongs_to_many :packages_in_destinations
has_many :destinations, through: :packages_in_destinations
end
# app/models/destination.rb file
class Destination < ApplicationRecord
validates_uniqueness_of :destination_title, scope: :destination_title
validates :destination_title, :description, :altitude, :geolocation, :presence=>true
has_and_belongs_to_many :packages_in_destinations
has_many :packages, through: :packages_in_destinations
end
# app/models/packages_in_destination.rb
class PackagesInDestination < ApplicationRecord
has_and_belongs_to_many :destinations, foreign_key: :destination_id, class_name: 'Destination'
has_and_belongs_to_many :packages, foreign_key: :package_id, class_name: 'Package'
end
the relationship between two table has been created from migration file
class CreatePackagesInDestinations < ActiveRecord::Migration[5.0]
def up
create_join_table :packages, :destinations, table_name: :packages_in_destinations do |t|
t.index :package_id
t.index :destination_id
t.timestamps
end
def down
drop_table :packages_in_destinations
end
end
another migration file is created to add a primary key to the table
class AddingPrimaryInPackaesInDestination < ActiveRecord::Migration[5.0]
def change
add_column :packages_in_destinations, :id, :primary_key
end
end
so from all these things no error has been shown but packages saves in packages table but not inserted relationship in the packages_in_destinations table. Please, somebody, suggest what's missing and what's wrong with these?
You need to either use has_and_belongs_to_many or has_many :through. Don't use both at the same time.
1) Example with has_and_belongs_to_many. In this case, you can remove PackagesInDestination model:
# app/models/package.rb file
class Package < ApplicationRecord
# ...
has_and_belongs_to_many :destinations, join_table: :packages_in_destinations
end
# app/models/destination.rb file
class Destination < ApplicationRecord
# ...
has_and_belongs_to_many :packages, join_table: :packages_in_destinations
end
# app/models/packages_in_destination.rb
# removed
2) Example with has_many :through:
# app/models/package.rb file
class Package < ApplicationRecord
# ...
has_many :packages_in_destinations
has_many :destinations, through: :packages_in_destinations
end
# app/models/destination.rb file
class Destination < ApplicationRecord
# ...
has_many :packages_in_destinations
has_many :packages, through: :packages_in_destinations
end
# app/models/packages_in_destination.rb
class PackagesInDestination < ApplicationRecord
belongs_to :destination
belongs_to :package
end

Rails searches the mistaken has_and_belongs_to_many table

I want to show all types which are related to a specific organisation in a select box of my document form. Types are part of the Ar engine. Organisations are part of another existing engine.
module Ar
module OrganisationPatch
extend ActiveSupport::Concern
included do
attr_accessible :ar_document_id
has_many :ar_documents, :class_name => 'Ar::Document'
has_and_belongs_to_many :ar_types, :class_name => 'Ar::Type'
end
end
end
module Ar
class Type < ActiveRecord::Base
attr_accessible :name
has_many :documents
has_and_belongs_to_many :organisations
end
end
class CreateTypeOrganisations < ActiveRecord::Migration
def change
create_table :ar_type_organisations, id: false do |t|
t.uuid :type_id, index: true
t.uuid :organisation_id, index: true
end
end
end
In my documents_controller I load types for forms about the before filter. The superior returns the organisation object:
def load_form_objects
unless current_user.admin?
#types = current_user.superior.ar_types
else
#types = Type.all
end
end
Calling the page I get this error and ask me why he is looking for a table called organisations_types:
ActiveRecord::StatementInvalid in Ar/documents#new
Mysql2::Error: Table 'portal.organisations_types' doesn't exist:
SELECT ar_types.* FROM ar_types INNER JOIN organisations_types
ON ar_types.id = organisations_types.type_id WHERE
organisations_types.organisation_id =
x'891c3986b33845d08d3951645a4f27d5'
Someone knows what I am doing wrong here?
Your table name isn’t map with lexical order what has_and_belongs_to_many expect. ( Expected order is organisations_types )
So you have to add :join_table option in both model's association. Like this,
has_and_belongs_to_many :ar_types, :class_name => 'Ar::Type', join_table: "ar_type_organisations"
has_and_belongs_to_many :organisations, join_table: "ar_type_organisations"
Reference

Join table in rails; many-to-many; trying to call join method on instance

I just did this a few hours ago and now I can't getting it working on a second attempt. There are many Users and many EventGoals. Users have many EventGoals and EventGoals have many Users, both through the join table called EventGoalClaims. The #event_goal is getting passed properly but I get a `
undefined method `event_goal_claims' for #<EventGoal:0x007fd11b0ce178>`
Here's the view (the first line is hitting an error):
<%= form_for([#event_goal, #event_goal.event_goal_claims.build]) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :user_last_name, :value => current_user.last_name %>
Controller
def create
#event_goal = EventGoal.find(params[:event_goal_id])
#event_goal_claim = #event_goal.event_goal_claims.build(eventgoalclaim_params)
#event_goal_claim.event_goal_id = #event_goal.id
#event_goal_claim.user_id = current_user.id
#event_goal_claim.user_last_name = current_user.last_name
...
private
def eventgoalclaim_params
params.require(:event_goal_claim).permit(:event_goal_id, :user_id, :user_last_name, :approved)
end
EDIT
class EventGoal < ActiveRecord::Base
belongs_to :event
has_many :users, through: :event_goal_claims
end
The answer likely lies in the associations of one of the three respective models. Make sure they look like this:
user.rb
Class User < ActiveRecord::Base
has_many :event_goal_claims
has_many :event_goals, through: :event_goal_claims
event_goal.rb
Class EventGoal < ActiveRecord::Base
has_many :event_goal_claims
has_many :users, through: :event_goal_claims
event_goal_claim.rb
Class EventGoalClaim < ActiveRecord::Base
belongs_to :user
belongs_to :event_goal
Also worth noting: This is all assuming your db has the foreign keys on the event_goal_claims table in your schema.rb labeled as user_id and event_goal_id. If they are different than that, you will need to explicitly state the name of the foreign key in the model's associations.

Associate foreign key through another foreign key

I've these 3 Models
class User < ActiveRecord::Base
has_many :answers, :as => :owner
end
class Answer < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
has_one :test
end
class Test < ActiveRecord::Base
belongs_to :answer
end
so I want to associate the Test model with User model through Answer Model without need of creating new association between them, so I put the following into Test Model:
has_one :owner, :through => :answer
but it doesn't work and I got this error
ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Test#owner' on the polymorphic object 'Owner#owner'.
any help?
In Test:
delegate :owner, :to => :answer
You have to specify source_type option as owner is a polymorphic association
class Test < ActiveRecord::Base
belongs_to :answer
has_one :owner, :through => :answer, :source_type => "User"
end

ActiveRecord Multi-association query

So I've got three models:
User
User Interest
Interest Tags
User Model
class User < ActiveRecord::Base
has_many :user_interests
end
InterestTag Model
class InterestTag < ActiveRecord::Base
has_many :user_interests, :dependent => :destroy
validates :name, :uniqueness => true
end
UserInterest Model
class UserInterest < ActiveRecord::Base
belongs_to :interest_tag
belongs_to :user
end
I'd like to use ActiveRecord to include the name of the user's interests when loading their profile using the following query:
#user = User.find(current_user.id, :include => [{:user_interests => :interest_tags}])
Migrations for interest_tags + user_interests
create_table :interest_tags do |t|
t.string :name, :null => false, :size => 30
t.timestamp :created_at
end
create_table :user_interests do |t|
t.integer :user_id
t.integer :interest_tag_id
end
What am I doing wrong?
You have to add an has_many :through association on User model.
class User < ActiveRecord::Base
has_many :user_interests
has_many :interest_tags, :through => :user_interests
end
class UserInterest < ActiveRecord::Base
belongs_to :interest_tag
belongs_to :user
end
class InterestTag < ActiveRecord::Base
has_many :user_interests, :dependent => :destroy
validates :name, :uniqueness => true
end
Now you can eager load the tags as follows:
User.find(current_user.id, :include => :interest_tags)
Note:
You might want to look at the acts_as_taggable_on gem for your requirement.
I assume you are building a tagging system, like in stackoverflow: every user kann have multiple tags that they are interested in. In that case the user_interests table is only a join table and does not need a model. Just use has_and_belong_to_many on the two real models.
See also this article on different ways to implement tagging with more or less normalized relational databases. You could also use a non-relational database like Mongodb, there you would need only one table to do tagging, see the cookbook.