I have a Mongoid model called "GradebookSettings". I've gone into inflections and added:
inflect.singular("GradebookSettings", "GradebookSettings")
When I go into irb, it properly singularizes "GradebookSettings" to "GradebookSettings". However, when I try to access an associated model, it keeps trying to singularize it to GradebookSetting.
I am using Mongoid. I am tempted to pluralize GradebookSettings with two s' but I'd rather not.
Thanks!
You don't need to trick the inflector. Use the :class_name option of the association to set the class instead:
embeds_many :gbsettings, :class_name => "GradebookSettings"
Related
I am trying to debug a big application running on RoR. In my model Text I have this relation :
has_many :copies, :foreign_key => :original_id
does this mean that there must be another model in my app called Copie (should be Copy...) to which it is linked through the foreign_key or could it be just a link to another table which uses the same model Text?
In most cases, yes. There would be a Copy model. You can point it to a different model though, by using the class_name option
Usually, if it has a reference to a different table, the class_name keyword would have been used. For example: has_many :copies, :class_name => 'YourOtherModel', :foreign_key => :original_id
By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix _id added.
Rails does provide you with the ability to set the :foreign_key option directly:
See documentation here
You can also design a data model to have to relation to itself:
See documentation here
I hope this provide you with some insight to further investigate
I am using Rails' generators to produce things like models in my app.
My models are commonly using the class_name option on relations.
Is it possible to generate a model from the command line and pass the value for class_name? I specifically want to avoid modifying the model after the generator runs.
An example of what I hope exists is something like:
rails generate model Book title:string author:belongs_to{class_name:User}
Then the generated Book model would look like:
class Book < ActiveRecord::Base
belongs_to :author, class_name: 'User'
end
No, you can't pass class_name as an option to the generator. It is not a valid option to the generator command. You can see the list of available options by running
rails g model --help
I believe the only way is to manually edit the models to specify the class_name
I have the following:
a Link model
a LinkItem model, which I want to be of the following type
a comment
a tag
...
I am using this code:
Link model
class Link < ActiveRecord::Base
has_many :link_items
end
LinkItem model
class LinkItem < ActiveRecord::Base
belongs_to :link
end
class Comment < LinkItem
end
class Tag < LinkItem
end
Now I don't know how to tell Rails that my LinkItem model is supposed to be polymorphic. I've read the Rails Guide on asociations and other tutorials, but these just describe how to create a belongs_to association to multiple other models, not the other way around.
So my question would be:
How do I create a has_many association where the associated instances can be of different types? Or would it be better to create seperate models for comments, tags, etc. and just associate each of them individually with my Link model?
EDIT
Actually my code works.
I just tried using a 'type'-column (instead of 'link_item_type') in my database and rails automatically used it to save/determine the correct subclass of my LinkItems (thanks Wizard of Ogz for the hint)
However I still can't access the subclasses of LinkItem without referencing a LinkItem first. Is this some kind of lazyloading?
If you are looking for polymorphic association nicholaides has the right way .
If you are looking for has_meny polymorphic association , check out the answer to "Setting up a polymorphic has_many :through relationship".
This is called a polymorphic association. Here is some documentation.
I just dealt with what I think is the same issue.
My filename for my model was wrong. I initially created it with one name (ex. link_tag.rb), and then changed the name of the class (ex. from LinkTag to Tag) on the fly without changing the name of the file (ex. tag.rb).
When I renamed the file correctly, it worked as expected.
In summary, the name of the file needed to match the name of the class.
I know this post is a little old, but maybe that will help someone someday!
I user polymorphic associations a lot!
I would first watch this RailsCast and then the documentation suggested by nicholaides.
It perfectly explains how to create both sides of the association.
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.
I have a model called produccion_alternativa.
I added a new inflection rule in config/initializers/inflections.rb, like this:
inflect.irregular('produccion_alternativa', 'producciones_alternativas')
I have other model called productor that has a relation with produccion_alternativa:
class Productor < ActiveRecord::Base
has_many :producciones_alternativas
class ProduccionAlternativa < ActiveRecord::Base
belongs_to :productor
When I try to get all the producciones_alternativas for a productor, I get this error:
irb(main):010:0> Productor.first.producciones_alternativas
NameError: uninitialized constant Productor::ProduccionesAlternativa
Any ideas?
I see several others having the same problem. Couldn't find an answer why this happens. So in the meantime you could just try this:
has_many :producciones_alternativas, :class_name => "ProduccionAlternativa"
Your Fail is that you pluralized both words in has_many association. You used:
has_many :producciones_alternativas
but based on the class name ProduccionAlternativa the plural is produccion_alternativas because only the last word is pluralized! So this should work:
has_many :produccion_alternativas
To check the Plural of a word type "your_word".pluralize in the rails console!
I found another solution too. I added another rule on inflection.rb:
inflect.irregular('ProduccionAlternativa', 'ProduccionesAlternativas')
inflect.irregular('produccion_alternativa', 'producciones_alternativas')
At least, now it's working as I want.