I have an rails application where I have scaffolded out an entire resource. Now through the UI I am trying to create an record but somehow i have an column in my database which is of type JSONB (Supported by PostGreSQL) whose value is not getting pushed into my table. Can anybody help me out on this as i am relatively new to rails.
I do not understand your intention of using the jsonb through the form.
you normally save a hash in jsonb column. So if you put hash like following in your text field it should get saved.
[{name: 'Jone', lastname: 'Doe'}].
Hope that helps
Related
I'm using Mongoid 4.0.0 with Rails 4. My models map tables in another application, and I have no control over the field names.
One of the models has a field named id, which is getting coerced into Mongo's _id field. For example, when I insert a document with an id value of "something" I get
{_id:"something", id:null}
instead of
{_id:ObjectId("<hexstring>"),id:"something"}
Is there any way to avoid this coercion, make Mongoid not conflate the two fields, and leave my id field alone?
As I said, renaming the id field is not an option.
Thanks!
[edited]
This is definitely not a MongoDB issue. It must be in Moped or (my guess) Mongoid.
I've tried changing the params key from :id to :_rid but this is still happening. I'm going to check out aliases, but from my first pass I don't think they're going to help -- they appear to go the wrong way.
This appears to be hardcoded into Moingoid and a pervasive assumption throughout. It's annoying enough, though, that I might come up with a patch to allow users to override the key field on a per-model basis.
Oh well.
I am currently working on a simple rails4 app. As part of the app, I am creating a form to populate the database and a particular column (:additional), I would like to populate with a hash where the key is a string (heading) and the value an array of strings (paragraphs below heading). So, for example: {"Heading" => ["Paragraph1", "Paragraph2"]} etc.
I am confused how I would now set up a form using rails to populate this column. I was thinking of creating a text_field for the title and then one or more text_areas underneath for the paragraphs and then somehow merging them in the controller but when creating the fields, I have to give the object as :additional which leads to problems.
How would I go about best accomplishing this? Is it even possible or should I restructure my database somehow?
Any advice is much appreciated.
If you're using postgres, ActiveRecord has support for using :hstore as the column type. If you're not, you can use serialize.
There is phone attribute in customer model with our rails 3.2.12 app. We would like to remove the phone attribute sometime when retrieving customers. Here is what we did:
#customers = Customer.all
#customers.delete :phone
However there is error:
delete_all doesn't support limit scope
What's the right way to remove an attribute from a model object? Thanks.
You can use Customer.select('name, address') to retrieve only the fields you want. Noting that you pass a string with comma separated field named.
This will generate an SQL request like this
SELECT customer.name, customer.address FROM customer
You then get only the data you want without deleting it from the database (which is what your original call is trying to do).
My original response showed incorrect use of pluck, which only works for a single column.
I know that you are using Rails 3.2.12, but in Rails 4 pluck also works with multiple columns, allowing something like
Customer.pluck(:name, :address)
I am using Rails 3.2.8 to build a "product set" builder that mirrors Google Analytics' Custom Profile builder. For example, a user may define a product set as follows:
(Category = 'Printers') and ((Name contains 'Wireless') or (Name contains 'Wifi'))
My product data is stored in Postgres (9.1.4) using an HStore column to store the dynamic product attributes. I have built a form that can construct the query using Arel but am stuck on the following requirements:
1. The query must be serialized to the database. I can store the .to_sql string but am then stuck with...
2. I must be able to reconstruct the user's form for later editing, as these are not one-time searches but rather shared queries.
How can I serialize in such a way that I can easily reconstruct the user defined query?
Couldn't you just serialize the parameters that rails gets from the form?
You'd have a hash with the keys and values from the user inputted form, and could easily feed it back into whatever logic you use to query the database from said form, or further process it.
You can have persistent model for saving the user's options about the search.
When you need to run the query, you will get the user's saved options and will pass them to custom method that use ransacker. You can access values in the hstore like this:
Arel::Node::InfixOperator.new('->>', 'hstore_column_name', Arel::Nodes::Quoted.new('key_in_hstore'))
I have an existing table that I'd like to use for a Rails application.
It's a simple table with only 4 columns. However it does not yet have id column. And also new data will be added periodically.
I am trying to find a way to add the id column and populate it.
I guess I have two options, but being a noob I am sure there are better ways.
Option 1: I can add the id column and populate it when I parse raw data into CSV files, and then import it to the Rails database. In this case, when I parse the data into CSV files, I need to figure out how to find the last used unique id is.
Option 2: Parse raw data into CSV files, then import to the Rails database. Then my rails application will populate the id column for the new data entries.
If Rails has a built in method or GEM that can populate the id fields for the new entries, that would be great. In that case I will go with the Option 2.
If not, I think it's easier to go with the Option 1.
So I guess the question becomes this: Can Rails automatically populate the id column of entries with blank id field?
Thanks!
Either option should work because the underlying database will automatically handle the id column since it's the primary key. So create the table using a migration, then parse, import, and add the CSV data to your database via which ever method sits best.