Ruby on Rails has many relation table - ruby-on-rails

I have a table rb which has many interfaces, then each interface has many interface_graph_data. Now my rb interface has_many relation seems to work fine, my only problem is the interface interface_graph_data relation. Here are my models.
class Rb < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :ip
validates_uniqueness_of :ip
validates_presence_of :username
validates_presence_of :password
has_many :interfaces
has_many :interfacegraphdata, :through => :interfaces
end
class Interface < ActiveRecord::Base
has_many :interfacegraphdata
end
class InterfaceGraphData < ActiveRecord::Base
end

The name of relation should be interface_graph_dates
upd:
You also missed
class InterfaceGraphData < ActiveRecord::Base
belongs_to :rb
belongs_to :interface
end
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

Related

An equivalent to first_or_create on a nested attribute in Rails 4

I'm using a has many through pattern with these 3 models
class User < ActiveRecord::Base
has_many :user_topics
has_many :topics, through: :user_topics
end
class Topic < ActiveRecord::Base
validates_presence_of :name
validates :name, :uniqueness => true
end
class UserTopic < ActiveRecord::Base
belongs_to :user
belongs_to :topic
accepts_nested_attributes_for :topic
end
At the moment a new topic model is trying to be created every time a new user_topic is created. I'd like to create a new topic model only if the topic name doesn't already exist, otherwise if it does, use the existing topic_id.
So something like:
class UserTopic < ActiveRecord::Base
belongs_to :user
belongs_to :topic
accepts_nested_attributes_for :topic, :first_or_create(:name)
end
Is it possible to do something similar to this?

How to use join to select many to many relationship model in ruby on rails4?

My data model is fellows, the note and hashtag's relationship is many to many
class Note < ActiveRecord::Base
attr_accessible :title
attr_accessible :content
attr_accessible :created_at
default_scope -> { order(created_at: :desc) }
has_and_belongs_to_many :hashtags
end
class Hashtag < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :notes
end
class NoteHashtag < ActiveRecord::Base
t.belongs_to :note, index: true
t.belongs_to :hashtag, index: true
end
I want to inquire the sql like:
select Note.* from Note inner join NoteHashtag on Note.id=NoteHashtag.note inner join Hashtag on NoteHastag.hashtag=Hashtag.id where Hashtag.name="test"
How to convert the sql into the datamodel operation in ruby on rails4?
I try to use the:
#note=Note.joins(:hashtag).where(name: "test")
and the error is:
ActionView::Template::Error (Association named 'hashtag' was not found on Note;perhaps you misspelled it?):
You need has_many :through associations if you are going to explicitly define the join model NoteHashtag. If you delete that model, you can do #notes=Note.joins(:hashtag).where(name: "test") and ActiveRecord will generate the query you are expecting.
Otherwise you could do
class Note < ActiveRecord::Base
...
has_many :note_hashtags
has_many :hashtags, through: :note_hash_tags
end
class Hashtag < ActiveRecord::Base
attr_accessible :name
end
class NoteHashtag < ActiveRecord::Base
belongs_to :note
end
Then #notes = Note.joins(:note_hashtags).joins(:hash_tags).where(name: "test) would work.
Note that both of these will return more than one note.
You can get records from many to many relationship by doing this:-
#note = Note.joins(:hashtags).where('hashtags.name' => 'test')
Why don't you address this relationship originating from the proper model? Define your models like this:
class Note < ActiveRecord::Base
...
has_many :note_hashtags
has_many :hashtags, through: :note_hashtags
end
class Hashtag < ActiveRecord::Base
attr_accessible :name
has_many :note_hashtags
has_many :notes through: :notes_hashtags
end
class NoteHashtag < ActiveRecord::Base
belongs_to :note
belongs_to :hashtag
end
And then query like this:
Hashtag.where(name: 'Test').notes

Rails Associations with Modules

With Rails 4.1 I can't seem to get my rails associations to work when using modules.
I have Objects within the FG module:
module FG
class Object < ActiveRecord::Base
belongs_to :user
has_one :email
has_one :phone
end
end
And Emails in the global space:
class Email < ActiveRecord::Base
belongs_to :object, class_name: 'FG::Object'
has_many :objects, class_name: 'FG::Object'
end
When I try
email.objects << object
I get the following error:
ActiveModel::MissingAttributeError
can't write unknown attribute `object_id'
Am I missing something in the association setup?
You could write your Email code this way:
class Email < ActiveRecord::Base
has_many :objects, class_name: 'FG::Object', foreign_key: 'email_id'
end
This will only work if you have an email_id in your objects table. You can not use has_many and belongs_to referring to the same class. That would mean you have an object_id in the one table and an email_id in the other.
You could also write:
class Email < ActiveRecord::Base
belongs_to :object, class_name: 'FG::Object', foreign_key: 'object_id'
end
That depends on your database construction.
I was thinking of the relationships in a conflicting way.
In order for the associations to make sense, I needed to organize them in the following way:
module FG
class Object < ActiveRecord::Base
belongs_to :user
belongs_to :email
belongs_to :phone
end
end
class Email < ActiveRecord::Base
has_many :objects, class_name: 'FG::Object'
end

How do I join multiple tables?

The case:
tables:
teacher :id :name
course :id :name
teachercourse :id :teacher_id :course_id
How to do inner join to this 3 tables with rails?
Edit (my models):
class Course < ActiveRecord::Base
attr_accessible :name
has_many :teachercourses
has_many :teachers, through: :teachercourse
end
class Teacher < ActiveRecord::Base
attr_accessible :name
has_many :teachercourses
has_many :courses, through: :teachercourse
end
class Teachercourse < ActiveRecord::Base
attr_accessible :course_id, :teacher_id
belongs_to :course
belongs_to :teacher
end
Edit2 - where I need the join result(show action):
class CourseController < ApplicationController
def show
#not real syntax
#course=Course.find(join:teacher,teachercourse,teacher :: where course='javacourse');
end
end
Both your Teacher and Course models should also contain has_many :teachercourses
Then, if you're writing your code in the Teacher model it should be something like this:
joins(teachercourses: :course)
Edit:
If I understand the intention behind the code you posted, you're looking for all the teachers that teach in the java course. So this should work:
Teacher.joins(teachercourses: :course).where(course: {name: "javacourse"})

Proper Rails association setup

Im setting up a reminder service that sends deals via email in relation to a persons interests AND city.. Basically, the user inputs important dates (friends bday, anniversary ect) and the interests of that special person.
I want to send them deals based on 1)the users city and 2)the interests of the related person
How should i setup my associations for the Deal model?
What i have so far..
class User < ActiveRecord::Base
belongs_to :city
has_many :person_interests, :as => :person
has_many :interests, :through => :person_interests
end
class City < ActiveRecord::Base
attr_accessible :name
belongs_to :province
has_many :users
end
class PersonInterest < ActiveRecord::Base
belongs_to :interest
belongs_to :person, :polymorphic => true
end
class Interest < ActiveRecord::Base
has_many :person_interests
end
Thanks!
If a deal could apply to more than one interest, you'd start with something like:
class Deal < ActiveRecord::Base
belongs_to :interests
belongs_to :city
end
class City < ActiveRecord::Base
attr_accessible :name
belongs_to :province
has_many :users
has_many :deals
end
class Interest < ActiveRecord::Base
has_many :person_interests
has_many :deals
end
And then you could do something like
#relevant_deals = #city.deals.where(:interest_id => 'abc')
or
#relevant_deals = #interest.deals.where(:city_id => 'def')

Resources