Has anyone successfully used the Rails acts_as_tenant gem for multitenancy, where the tenant ID column is not named xxx_id?
my model is something like
has_one :tenant, :foreign_key => "tenant_code"
acts_as_tenant :tenant
Then I get my query select * from [table] where tenant_id = xxx ignoring the overridden foreign_key spec.
Is there any way to fix this? or am I doing something else obviously wrong?
Thanks!!
Just as an update to this issue for future reference:
From gem version 0.3.3 onwards it is now possible to explicitly set the foreign key.
acts_as_tenant(:account, :foreign_key => 'accountID')
Perhaps this helps someone who finds this question.
ActsAsTenant sets it's own foreign key in the gem itself as (pseudocode) #{tenant_klass}_id:
def self.fkey
"#{##tenant_klass.to_s}_id"
end
https://github.com/ErwinM/acts_as_tenant/blob/master/lib/acts_as_tenant/model_extensions.rb#L12-L14
Unfortunately, I don't believe you can set your own foreign key for anything other than activerecord model relationships (with your code, you could still theoretically call model.tenant; however, that doesn't help you).
Related
I have a basic database with a structure like this.
products
------------------
id
serial
order
------------------
id
product_serial
Unfortunately, I cannot change structure of the DB. I looked at the docs for Rails 2.1 and it said I could setup a relationship like this.
belongs_to :product,
:class_name => 'Product',
:foreign_key => 'product_serial',
:primary_key => 'serial'
However, that gives me this error.
Unknown key: primary_key
Without the primary key it produces this SQL
SELECT * FROM `products` WHERE (`products`.`id` = #{serial})
How do I setup a belongs_to relationship on this?
EDIT For the record, I am working in Rails 2.1. (I know, don't tell me).
If you check the available options for the belongs_to association for the Rails 2 branch, you'll see that :primary_key is not one of them.
It should be enough, in your case, to simply state the foreign key as you did in the previous line.
Your order model does not have a field (and thus method) which is called serial. You don't need to specify it, then it defaults to id (which you do have).
I am using mongoid with devise invitable,
after assigning roles to user I found the following error
"**undefined method `as_document' for Array **" , any suggestions ?
invitable = find_or_initialize_with_error_by(:email, attributes[:email])
invitable.attributes = attributes
# scope_id attribute does not set properly
invitable.roles.map {|r| r.scope_id = attributes[:roles_attributes]["0"][:scope_id]}
if invitable.persisted? && !invitable.invited?
invitable.errors.add(:email, :taken)
elsif invitable.email.present? && invitable.email.match(Devise.email_regexp)
invitable.invite!
end
Whats wrong I am doing ?
This is likely because as_document doesn't work against an array, only single objects.
This is a bug with Mongoid and has_many relationships.
The method 'as_document' has to be defined for has_many relationships, like it is defined for embeds_many relationships.
I am going to make a pull request to have this issue fixed, in the meantime you can define mongoid in your gemfile like so:
gem 'mongoid', :git => https://github.com/mrjlynch.git
That happened to me with MongoId 5.1.0 when, in one direction I had "embeds_many", and in the other direction I had "belongs_to".
From what I know, the reverse of embeds_many shall be embedded_in.
Changing the reverse relationship to embedded_in fixed that issue for me.
I have to admit, this is a very obscure error message.
We recently upgraded our rails app from version 3.0.3 to 3.1.0. The application runs successfully for the most part as it did before with one major exception. We have a many-to-many relationship between two models, SurveyDatum and SubGroup, joined via a model called SubGroupSurveyDatum. Here is the code for each:
class SurveyDatum < ActiveRecord::Base
has_many :sub_group_survey_data
has_many :sub_groups, :through => :sub_group_survey_data
end
class SubGroup < ActiveRecord::Base
has_many :sub_group_survey_data
has_many :survey_data, :through => :sub_group_survey_data
end
And as you might expect:
class SubGroupSurveyDatum < ActiveRecord::Base
belongs_to :survey_datum
belongs_to :sub_group
end
If I have a SurveyDatum object that I retrieved previously from the database (lets call it 'sd'), and I invoke the sub_groups method (sd.sub_groups), this is the resulting sql query generated by active record:
SELECT `sub_groups`.* FROM `sub_groups` INNER JOIN `sub_group_survey_data` ON `sub_groups`.`id` = `sub_group_survey_data`.`sub_group_id` WHERE `sub_group_survey_data`.`survey_datum_id` IS NULL
The "IS NULL" part is obviously where the id of my survey data object is supposed to go, however active record fails to use it. The object does indeed have an id, since as mentioned it was persisted and retrieved from the database. This problem only cropped up after we moved to rails 3.1, so I assume there's something I've not done properly in accordance with the new version, but I have no idea. Any ideas? Thank you in advance for your help!
Hmm I used rails 3.1.0 and tried to replicate but all was well. The only case was when I manually set id = nil on the record retrieved from the db. Then I got:
SELECT "authors".* FROM "authors" INNER JOIN "relations" ON "authors"."id" = "relations"."author_id" WHERE "relations"."post_id" IS NULL
What database are you using? I was trying this with sqlite3. Also watch out for certain gems especially those that work with ActiveRecord. I had trouble with this in the past.
We discovered the issue. I had forgotten that the survey_data table has a composite primary key. When we upped to version 3.2.3, and added in the SurveyDatum model the following:
set_primary_key :id
The query finally built and executed properly.
I need to associate two models with a simple has_many. The problem is that I don't want to use the id (_id) as the primary key for the association. I still want the model to keep using the default ObjectIds for everything else.
(This is running on Rails3.1 + Mongoid)
So basically I want:
class Message
...
field :message_id, :default => proc { "fail-#{Time.now.to_f.to_s}" }
...
has_many :message_reports, primary_key: :message_id, foreign_key: :message_id
...
end
class MessageReport
...
field :message_id, :default => proc { "fail-#{Time.now.to_f.to_s}" }
...
has_many :message, primary_key: :message_id, foreign_key: :message_id
...
end
This would only work for ActiveRecord. Mongoid don't support the primary_key option.
So how do I get the same results for Mongoid collections?
Before you say: don't do that...
The reason I really really need to kay on this field and not the proper id is that these are messages... and the message_ids are unique ids returned by the API I call to send a message. Later the same id is received in callbacks from the other side.
I could just do queries and stick it in a method to find the "associated" reports from a message and vice versa... I'd rather have them be actual associations, if possible.
I could force the report-recieving process to search for and match up the objects for the association... but I'd rather not put that responsibility there when it is kind-of superfluous and it has nothing more to do with this data besides validating and saving it.
In short: I'd prefer an association :)
This feature doesn't exist on Mongoid actually even on Master and it's not planned in Mongoid 3.0
Do some feature request. The Mongoid community is really open to add some new feature if it's a good idea. To me It's a good idea.
I have a model named Person. It has two properties - name and parent_person_id
A person will always have a parent person.
Should I be using belongs_to in the model? If so, what are the advantages of doing so.
class Person < ActiveRecord::Base
belongs_to :person
end
I've not tried this code out yet, it seems a bit wrong my normal mysql ways.
I'm looking for opinions here more than anything, I'm quite new to the rails and want to make sure I'm doing things properly, doing things 'the Rails way'.
I'd suggest using a gem like ancestry for a tree structure like that. It gives you your association plus lots of utility methods (finding parent, children, siblings, retrieving a subtree).
If you don't want that, then in your belongs_to association has to look like this:
belongs_to :person, :foreign_key => "parent_person_id"
since without that option, rails would look for a foreign key of person_id and, not finding that, light your CPU on fire throw an error message.
Yes, you would need that belongs_to since this is what will tell rails about this relationship.