How can I deal with mongoid documents inside namespaces?
I have two mongoid documents as follow:
module A
module B
Class User
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :groups, :cascade => :nullify, :class_name => 'A::B::Group'
end
Class Group
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :users, :cascade => :nullify, :class_name => 'A::B::User'
end
end
end
The problem raises when I try to delete a group that contains an user:
u = User.create()
g = Group.create()
g.users << u
at this point u.groups_ids contains the _id of the group g, but when I perform:
g.destroy
It complains of a missing method called 'A/B/groups_ids' in class 'User'.
It is trying to remove the reference of g inside u, but it fails to find the correct namespace...
If I remove the namespaces A and B and the :class_name options everything works fine.
Which is the correct way (if any) to handle namespaces in this scenario?
the solution is to add a :foreign_key => 'A/B/groups_ids':
Class User
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :groups, :cascade => :nullify, :class_name => 'A::B::Group', :foreign_key => 'A/B/group_ids'
end
Class Group
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :users, :cascade => :nullify, :class_name => 'A::B::User', :foreign_key => 'A/B/user_ids'
end
Related
I'm trying to gain a value from an associated table linked via a field in a table using the following (where i is created in an each loop):
value = current_user.question["convq#{i}"].id
My table has references from the Questions table to a Questionsbank table in the form of columns named convq1_id, convq2_id ... convq3_id.
I simply want to cycle through each field in Questions and gain the questionbank.text field.
Sadly, the above will only give me the physical value in the question table field not a link to the associated record using the following code:
(1...9).each do |i|
value = current_user.question["convq#{i}"].id
end
Can anyone let me know how I can cycle through table and gain the associated 'id' or text value of the referenced record?
Thanks in advance.
Each model is set up as:
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :convq1, :class_name => 'Questionbank'
belongs_to :convq2, :class_name => 'Questionbank'
belongs_to :convq3, :class_name => 'Questionbank'
belongs_to :convq4, :class_name => 'Questionbank'
belongs_to :convq5, :class_name => 'Questionbank'
belongs_to :convq6, :class_name => 'Questionbank'
belongs_to :convq7, :class_name => 'Questionbank'
belongs_to :convq8, :class_name => 'Questionbank'
belongs_to :convq9, :class_name => 'Questionbank'
has_many :questionbanks
end
and ...
class Questionbank < ActiveRecord::Base
has_many :likes
has_many :questions
end
I think this is what you might be looking for:
(1...9).each do |i|
value = current_user.question["convq#{i}"]
questionbank = Questionbank.find_by( question: "{value}")
questionbank.id
end
I'd like to be able to reference a model (a has_many relationship) more than once in the same model. For example, given the following models:
class MyModel
include Mongoid::Document
field :name, type: String
has_many :main_efforts, :class_name => 'Effort', as: :effortable, dependent: :delete, autosave: true
has_many :secondary_efforts, :class_name => 'Effort', as: :effortable, dependent: :delete, autosave: true
validates_presence_of :name
end
class Effort
include Mongoid::Document
field :name, type: String
belongs_to :effortable, polymorphic: true
validates_presence_of :name
end
As you can see, the Effort model is referenced twice. Originally, my Effort model wasn't polymorphic, but it seemed Mongoid was unable to determine which collection (main_efforts or secondary_efforts) the effort belonged to. As such, I made it polymorphic. After making it polymorphic, however, my main_efforts and secondary_efforts fields are always an empty array.
What is the proper way to reference a polymorphic model more than once in the same model (assuming a polymorphic model is necessary)?
Figured it out:
class MyModel
include Mongoid::Document
field :name, type: String
has_many :main_efforts, :class_name => 'Effort', dependent: :delete, autosave: true, :inverse_of => :main_effort
has_many :secondary_efforts, :class_name => 'Effort', dependent: :delete, autosave: true, :inverse_of => :secondary_effort
validates_presence_of :name
end
class Effort
include Mongoid::Document
field :name, type: String
belongs_to :main_effort, :class_name => 'Conop', :inverse_of => :main_efforts
belongs_to :secondary_effort, :class_name => 'Conop', :inverse_of => :secondary_efforts
validates_presence_of :name
end
When you have a relation such as embeds_many :album_items which relates to the AlbumItem model. How can I have it stored in just items. I tried embeds_many :album_items, :as => :items and embeds_many :items, :class_name => AlbumItem. Neither worked.
How can I go about renaming the relation?
Thanks
Does this work(assuming your parent model name is Album)?
In Album:
embeds_many :items, :class_name => "AlbumItem", :inverse_of => :album
and in AlbumItem:
embedded_in :album, :class_name => "Album", :inverse_of => :items
Hi guys I have a class like below for a crawler model:
class Link
include Mongoid::Document
include Mongoid::Timestamps
field :url, type: String
field :links, type: String
index :url
has_many :pages
end
where a link repent a URL and they have many inbound/outbound connections, I would like to have it working, so:
a_link.links # => gives a list of outbound link objects.
How would you do it with mongoid?
You can set up a many-many association using has_and_belongs_to_many on each side of the relationship.
class Link
include Mongoid::Document
has_and_belongs_to_many :links, :class_name => 'Link', :inverse_of => :inbound_links
has_and_belongs_to_many :inbound_links, :class_name => 'Link', :inverse_of => :links
end
As the association is to and from the same class in this case you need to give mongoid a little help with the class_name and inverse_of because it can't infer this from the association name.
a bit cleaner way to archive this using many-many associations
class Link
include Mongoid::Document
has_and_belongs_to_many :links, class_name: 'Link', inverse_of: :links
end
There is a model relation like this.
class A
belongs_to :ref_config,:class_name => 'User'
end
My question is :
the A has a attribute named flag, now i want to create a function like this:
if flag == 1, I want the class A like this belongs_to :ref_config,:class_name => 'Department and if flag == 2, i want the class A like this belongs_to :ref_config,:class_name => 'User'
How can I implement the function
Thank you!
Have a look at polymorphic associations, which will let you use the same belongs_to relation to refer to different models.
You could configure your models something like this:
class A < ActiveRecord::Base
belongs_to :ref_config, :polymorphic => true
end
class Department < ActiveRecord::Base
has_many :as, :as => :ref_config
end
class User < ActiveRecord::Base
has_many :as, :as => :ref_config
end
To set up the needed columns in the A table, use a migration like this:
class CreateAs < ActiveRecord::Migration
def self.up
create_table :as do |t|
t.string :name # or whatever other attributes A should have
t.references :ref_config, :polymorphic => true
end
end
def self.down
drop_table :as
end
end
From the little i got your question following may help you.
class A
belongs_to :department_config, :class_name => 'Department', :conditions=> flag= 1
belongs_to :user_config, :class_name => 'User', :conditions=> flag= 2
end