project isn't complicated, but finding my way around mongoid relationships has been. It would be nice to find some in depth explanation.
I couldn't make my code work however I set them up so I managed to find Mongoid_Alize: https://github.com/dzello/mongoid_alize
Yet when I do it with alize or alize_to/_from, console gives me back an error on Mongoid::Relations in Ruby2.3.3/lib/ruby/gems/2.3.0/bundler/gems/mongoid_alize-b9175f4e3165/lib/mongoid/alize/macros.rb:79:in `_alize_relation_types'"
I have also included eager loading with identity_map_enabled: true in mongoid.yml
So here's the code:
Currency.rb
class Currency
include Mongoid::Document
include Mongoid::Alize
field :name, type: String
field :state, type: String
field :code, type: Integer
field :unit, type: Integer
has_many :currency_value
has_many :value, :class_name => 'Value', :inverse_of => :currency
alize_to :value
end
Value.rb
class Value
include Mongoid::Document
include Mongoid::Alize
field :date, type: DateTime
field :buying, type: String
field :middle, type: String
field :selling, type: String
belongs_to :value_currency, :class => 'Currency', :inverse_of => :currency_value
belongs_to :currency, :class_name => 'Currency', :inverse_of => :value
alize_from :currency
end
ApiService.rb
class ApiService
# Has some api data coming from external function getApiData
def update()
#arrayOfHashes= getApiData() #array of hashes
#arrayOfHashes.each do |hash|
#currency = Currency.new({
name: hash["Name"],
state: hash["State"],
code: hash["Currency code"],
unit: hash["Unit"]
})
#currency.create_value(
date => hash["Date of value"],
buying => hash["Value when buying"],
middle => hash["Middle value"],
selling => hash["Value when selling"])
#currency.save
end
return #currency
end
end
Expected result: save all 13 iterations in MongoDB with their relations.
It would be much obliged to hear some explanation here cause I'm uninspired and stuck in place. I have some background in programming and i have a feeling that it shouldn't be that complicated.
I can save either Currency model or Value but not both with their relations. Other idea is to do it manually by adding a custom foreign_id and making a method in the model which will populate hash. But that could lead to future performance issues.
Any help is appreciated, thank you :)
Related
I have an application where users can create many travels, and they can invite their facebook friends. In the travel document, there is a field "participants" that is an embedded document, Participant model embedded in Travel model.
Here are my models :
class Travel
include Mongoid::Document
include Mongoid::Timestamps
# relations
belongs_to :user
# fields
field :title, type: String
field :description, type: String
field :begin_date, type: Date
field :end_date, type: Date
field :budget, type: Integer
field :go_back, type: Boolean
field :title_namespace, type: String
# friends
embeds_many :participants
accepts_nested_attributes_for :participants
end
class Participant
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :user_id, type: String
# relations
embedded_in :travel, :inverse_of => :participants
end
When I try to display travel where users have been invited, with this request :
#travel_participations = Travel.where('participants.user_id' => #user.id)
I don't have any result, even if I have this line in byebug :
#<Mongoid::Criteria
selector: {"participants.user_id"=>BSON::ObjectId('592c8da58511989ec850921e')}
options: {}
class: Travel
embedded: false>
So when I put this on my view :
<% unless #participations.nil? %>
<% #travel_participations.each do |travel_participation| %>
<p> <%= travel_participation.title %> </p>
<% end %>
<% end %>
I tried with .all, .first, .to_a, .as_json, no results ... Some one know where is the problem ?
You have this in your embedded model:
field :user_id, type: String
but your query is using a BSON::ObjectId:
Travel.where('participants.user_id' => #user.id)
as shown in the raw query:
selector: {"participants.user_id"=>BSON::ObjectId('592c8da58511989ec850921e')}
Your embedded document probably has a string field like:
"user_id": "592c8da58511989ec850921e"
rather than the ObjectId you're looking for:
"user_id": ObjectId("592c8da58511989ec850921e")
so you won't find what you're looking for due to the type mismatch.
Either fix the embedded field's type:
field :user_id, type: BSON::ObjectId
or query it as the string it is:
Travel.where('participants.user_id' => #user.id.to_s)
Changing the type will involve fix up whatever data you already have, changing the query is ugly in a different way.
Sometimes Mongoid will convert between strings and ObjectIds for you, sometimes it won't. When I used Mongoid I patched to_bson_id methods into BSON::ObjectId, String, Mongoid::Document, ... so that I could say things like:
Model.where(:some_id => some_id.to_bson_id)
and not have to constantly worry about what type some_id was. I also made sure that all ID fields were always specified as BSON::ObjectId.
I am using Rails 3 with mongoid 2. I have a mongoid class forum, which embeds_many topics.
Topics embeds_many forumposts
When I try to save a forumpost doing the following in my controller...
#forum = Forum.find(params[:forum_id])
#forum.topics.find(params[:topic_id]).forumposts.build(:topic_id => params[:forumpost][:topic_id], :content => params[:forumpost][:content], :user_id => current_user.id,:posted_at => Time.now, :created_at => Time.now, :updated_at => Time.now)
if #forum.save
On save I get...
undefined method `each' for 2012-11-14 23:15:39 UTC:Time
Why am I getting that error?
My forumpost class is as follows...
class Forumpost
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
field :content, type: String
field :topic_id, type: String
field :user_id, type: String
field :posted_at, type: DateTime
attr_accessible :content, :topic_id, :user_id, :posted_at, :created_at, :updated_at
validates :content, presence: true
validates :topic_id, presence: true
validates :user_id, presence: true
belongs_to :topic
belongs_to :user
end
There is alot wrong/wierd with your example code, so lets see if we can start at the start:
You say forum embeds many topics, which embeds many posts. But your model is using a belongs_to association. Belongs_to is used for references which are different than embedded documents. If your Topic model has this:
class Topic
...
embeds_many :forumposts
...
end
Then your Forumpost model should have this:
class Forumpost
...
embedded_in :topic
...
end
Read up on references vs embedded documents here: http://mongoid.org/en/mongoid/docs/relations.html
Next point, You don't need to put :topic_id into the forumpost since you are building it off the topic.
Next point, don't save the forum, save the forumpost. And instead of doing a build followed by a save, try just doing it as a create in one go.
Next point, instead of setting user_id => current_user.id, try setting user => current_user. This is the magic that the belongs_to association provides... its cleaner and avoids messing around with IDs.
Next point, why do you need both created_at (supplied by Mongoid::Timestamps) and posted_at ?
Last point, you shouldn't need to set the timestamps, they should be set automatically when created/updated (unless for some reason you actually need posted_at).
Try something more like this:
#forum = Forum.find(params[:forum_id])
#topic = #forum.topics.find(params[:topic_id])
if #topic.forumposts.create(:content => params[:forumpost][:content], :user => current_user)
#handle the success case
else
#handle the error case
end
I'm having some problems trying to understand how Mongoid does its sorting. I have 2 models, Gig and Venue, both of which are associated by a belong_to has_many relationship.
I'm trying to sort objects from Gig by the attribute 'name' of the Venue Object to no avail.
I'm hoping someone out there would be able to point me to the right direction.
Below are a truncated model description.
My Query is also below:
# Gig Model
class Gig
include Mongoid::Document
include Mongoid::Paperclip
include SearchMagic
belongs_to :owner, :class_name => "User", :inverse_of => :owns
belongs_to :venue
has_and_belongs_to_many :attenders, :class_name => "User", :inverse_of => :attending
has_and_belongs_to_many :artistes
<snip>
end
# Venue Model
class Venue
include Mongoid::Document
include Mongoid::Paperclip
include SearchMagic
has_many :gigs
field :foursquare_id, type: String
embeds_one :address
embeds_many :user_ratings
field :facebook, type: String
field :twitter, type: String
field :website, type: String
field :name, type: String
field :postal, type: String
field :tel, type: String
field :venue_type, type: String
field :description, type: String
field :rating, type: Float, default: 0.0
<snip>
end
# Console
>> Gig.desc('venue.name').map{|f| f.venue.name}
=> ["*SCAPE", "Velvet Underground", "Blujaz Lounge", "Velvet Underground", "Home Club", "Wh
ite House, Emily Hill", "Zouk", "Zouk", "The Pigeonhole", "Home Club", "Home Club", "Home C
lub"]
# sorting doesn't work
You can't join in mongo. If you need joins, use a relational database. A "feature" of non-relational databases is that you can't do joins.
You have basically two choices:
a before_save callback, which will inject the name of the venue into the gig as an additional field (see for instance https://github.com/rewritten/timebank/blob/master/lib/mongoid/denormalize.rb)
a map-reduce task, which after any modification of any venue or gig, will update the venue name into the gig as an additional field.
In the end, you need a field in the Gig collection to order it.
I have 2 models with nested data:
class Goodtender
include Mongoid::Document
include Mongoid::Timestamps
field :name
field :count
references_many(:offerprices, :autosave => true)
accepts_nested_attributes_for :offerprices, :allow_destroy => true, :reject_if => :all_blank
validates_presence_of :name, :message => "Invalid"
validates_numericality_of :count, :message => 'Invalid'
validates_associated :offerprices, :message => 'Invalid'
end
class Offerprice
include Mongoid::Document
include Mongoid::Timestamps
field :summ
field :date_delivery, :type => DateTime
field :note
referenced_in :goodtender, :class_name => 'Goodtender'
validates_presence_of :date_delivery, :message => "Invalid"
validates_numericality_of :summ, :message => 'Invalid'
end
When making nested records, correct validation takes place, for example, if data in nested model do not correct, so command:
#tender = Tender.new(params[:tender])
#tender.save
returns false
but if update data:
#tender = Tender.find(params[:id])
#tender.update_attributes(params[:tender])
always eturns true
Even if nested data do not valid. Here parent's data updates and valids and if parents' data does not valid returns false, if one of the nested record does not valid,
they are ignored when you are saving and update_attributes returns true. Is there the opportunity to check data on validity in the time of updating all the nested data chain? Thank you for your respond.
I'm using:
Ruby 1.8.7
RoR 3.0.9
Mongoid 2.0.1
Please check "valid" function to each model for validate.
Please add code as per below in your code :
#tender = Tender.find(params[:id]) <br/>
#tender.fieldname=params[:name] <br/>
if #tender.valid? <br/>
#tender.save <br/>
end <br/>
I've not seen this feature as a plug in and was wondering how to achieve it, hopefully using rails.
The feature I'm after is the ability to rate one object (a film) by various attributes such as plot, entertainment, originality etc etc on one page/form.
Can anyone help?
I don't think you need a plugin to do just that... you could do the following with AR
class Film < ActiveRecord::Base
has_many :ratings, :as => :rateable
def rating_for(field)
ratings.find_by_name(field).value
end
def rating_for=(field, value)
rating = nil
begin
rating = ratigns.find_by_name(field)
if rating.nil?
rating = Rating.create!(:rateable => self, :name => field, :value => value)
else
rating.update_attributes!(:value => value)
end
rescue ActiveRecord::RecordInvalid
self.errors.add_to_base(rating.errors.full_messages.join("\n"))
end
end
end
class Rating < ActiveRecord::Base
# Has the following field:
# column :rateable_id, Integer
# column :name, String
# column :value, Integer
belongs_to :rateable, :polymorphic => true
validates_inclusion_of :value, :in => (1..5)
validates_uniqueness_of :name, :scope => :rateable_id
end
Of course, with this approach you would have a replication in the Rating name, something that is not that bad (normalize tables just for one field doesn't cut it).
You can also use a plugin ajaxfull-rating
Here's another, possibly more robust rating plugin...it's been around for a while and has been revised to work with Rails 2
http://agilewebdevelopment.com/plugins/acts_as_rateable