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)
Related
I have 3 models: movies, movie_tags and movie_tag_counts
It is a classic has many through relationship. My use case is that every movie can have multiple tags and user can vote on tags that were already added.
My Problem is that I can't seem to update an existing object in movie_tag_counts
movie_tag_count = MovieTagCount.first
movie_tag_count.count += 1
movie_tag_count.save
the result is this error message
TypeError: nil is not a symbol nor a string
My best guess is that the reason is that movie_tag_counts table doesn't have an id column of its own, but I still have no idea how to fix it.
My current workaround is to execute a sql statement directly
Turns out my guess was right, ActiveRecord expects an id column, I added it like this
add_column :movie_tag_counts, :id, :primary_key
and everything worked perfectly. I'm sure there's a way to do it without the id column by overwriting some AR methods, but I guess having another column won't hurt that much
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.
Is there a way to restrict what hstore columns can be saved? I've got below code for doing this so far:
store_accessor :widget_locations, :left_area1, :mid_area1, :left_area2, :mid_area2, :right_area2
but this seems to still allow other key names to be saved ie. middle_area123
Also how am I able to update hstore like update_attributes or update?
I could be wrong but my guess is that you are making calls on widget_locations like
item.widget_locations[:left_area1] = thing
If so, you should change that to
item.left_area1 = thing
because you told the store_accessor to create attributes :left_area1, :mid_area1, :left_area2, :mid_area2, :right_area2 that will be serialized to database column :widget_locations. Now these attributes will behave like normal attributes, so you can put validations on them etc.
This also allows you to update an item as usual:
item.update(name: 'Test', left_area: 'garden', mid_area: 'livingroom')
The catch with a hstore is that accessing the serialized column will allow you to add new unknown attributes, so it is best to directly access the attributes you explicitly specified.
I'm trying to turn a database record into an exportable document, such that I can reimport it and update the database. I'd just use CSV but it's also nested, say Country has_many Provinces has_many Cities.
I've tried dumping YAML per this earlier question:
File.open("#{RAILS_ROOT}/lib/tasks/canada.yml", 'w') do |file|
country = Country.find(1)
country.provinces
country.cities
YAML::dump(country, file)
end
But when I load, it doesn't:
country = YAML.load_file("#{RAILS_ROOT}/lib/tasks/canada.yml")
I even tried hacking the new_record flag per this article but it doesn't change anything. I'm using Rails 3.x.
This must be something other people have done before. How? Or do I need to approach the problem differently?
I guess the problem is that country.save will not produce a new record in the database? You can try Country.create!(country.serializable_hash). That will at least create a new record based on the attributes of country. The country id will be changed and I guess nested objects (Province, City) will not be handled properly.