Mongoid Booleans should default to checkbox #732

Closed
johnnyshields opened this Issue Jan 19, 2013 · 14 comments

Comments

Projects
None yet
3 participants

Hi, I'm not sure if Mongoid is supported officially as an ORM, I presume it is because I'm using it in production already.

If so, please note that Mongoid boolean fields do not currently automatically default to checkboxes as they do with ActiveRecord. I have to add as: boolean to all my views.

# my_model.rb
field :is_active, type: Boolean
# my_form_view.html.haml
=f.input :is_active, as: :boolean
Collaborator

carlosantoniodasilva commented Jan 19, 2013

We don't officially support mongoid, as you can see in #635, but people have been using it with success for some time already. The main difference is with collections, that we cannot load automatically as we do with Active Record. Other than that, everything should be good, but apparently booleans are also a special case.

Do all other types work fine? Integers, Strings, Texts, and so on? I don't mind changing the code slightly to support at least this boolean functionality, if that's the only one that fails to be automatically picked up.

From what I can tell it's only booleans that have the problem. I converted my app from ActiveRecord to Mongoid and booleans seem to be the only place where I had to touch the SimpleForm view code.

Collaborator

carlosantoniodasilva commented Jan 19, 2013

@johnnyshields interesting, ok then. I'll leave this open for a while, but since we're not big Mongoid users, I can't promise we'll be changing anything to fix it, so if anyone wants to take a stab, is more than welcome.

farnoy commented Jan 23, 2013

From what I can tell it's only booleans that have the problem. I converted my app from ActiveRecord to Mongoid and booleans seem to be the only place where I had to touch the SimpleForm view code.

SimpleForm does not work with Mongoid at all. Every input is interpreted as :string and you have to provide the :as option explicitly - it's not just booleans.

Collaborator

carlosantoniodasilva commented Jan 23, 2013

@farnoy we handle input types based on column definitions, if mongoid cannot provide us with the same structure, so it def does not work the same way :). Thankfully using :as as you said, is enough to have it fully working :).

farnoy commented Jan 23, 2013

I was just trying to clarify that it's not just booleans that don't work :)

Collaborator

carlosantoniodasilva commented Jan 23, 2013

Yeah that's what I imagined, thanks for making things clearer 😃. We've had lots of attempts on making SimpleForm work more seamlessly with Mongoid, with no success yet, and since we're not big mongoid users, we are not going to do that ourselves, but we can help with anything necessary to make it happen.

farnoy commented Jan 23, 2013

we handle input types based on column definitions, if mongoid cannot provide us with the same structure, so it def does not work the same way :). Thankfully using :as as you said, is enough to have it fully working :).

By the way, an example on devise-generated models

Mongoid

User.fields["email"].options[:type]
#=> String

ActiveRecord

User.columns.select {|c| c.name == "email" }.first.type
#=> :string 

It's not a great difference, can't we unify that? Associations shouldn't be any harder, since Mongoid provides pretty Metadata objects for them too.

farnoy commented Jan 23, 2013

For a proof of concept, we could override this and define some intermediate type that would mimic the AR behavior.

Collaborator

carlosantoniodasilva commented Jan 23, 2013

an example on devise-generated models

SimpleForm != Devise :)

We've had a few headaches to make it work the way it is in Devise for both AR and Mongoid users, I don't want to have similar headaches here as well.

It's not a great difference, can't we unify that?

Sure we can, I'm just saying we're not gonna do any work toward that until we have the need.

As I said, if anyone wants to give it another try, we're more than happy to provide support. But please, before getting started, make sure to get a feeling on some of the other attempts: #106 #187 #140 #417 #410 #153 #281 #268, search for more.

farnoy commented Jan 23, 2013

They seem to target mongoid 2 and mongodb < 2.0, I think there were major changes with the new version so maybe #fields wasn't available at that time. I don't currently have time, but I will try to do something later. Mongoid generates the same accessors, so only type of the field recognition is needed for simple fields to work properly. Meanwhile, thanks for the information, I think this issue can be closed now - it's not about booleans.

Collaborator

carlosantoniodasilva commented Jan 23, 2013

Ok, thank you @farnoy, and please feel free to ping us if you decide to get your hands on something :).

Cool thanks guys, apologies for my lack of knowledge in the initial post. 🍺

Collaborator

carlosantoniodasilva commented Jan 24, 2013

@johnnyshields no worries, it's always good to bring this subject back, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment