i have two fields in my model and i want add them in Soulmate::Loader:
for example, my "person" model has name and email field. and i want load then in Soulmate:
loader = Soulmate::Loader.new("people")
loader.add("term" => name, "id" => self.id, "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
i want add name and email in loader.add. but i can't.
def load_into_soulmate
loader = Soulmate::Loader.new("people")
loader.add("term" =>{ name , email }, "id" => self.id, "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
end
and
def load_into_soulmate
loader = Soulmate::Loader.new("people")
loader.add("term" =>{ "name" => name ,"email" => email }, "id" => self.id, "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
end
error show when i use Person.find_each(&:save) for add datas to redis:
ArgumentError: ArgumentError
from /var/lib/gems/2.2.0/gems/soulmate-1.1.0/lib/soulmate/loader.rb:31:in `add'
but all is wrong.
I think you have to use aliases.
def load_into_soulmate
loader = Soulmate::Loader.new("people")
loader.add("term" =>"name", "id" => self.id, "aliases" => [email], "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
end
Related
I am practicing with apis in RoR. I am trying to save only a few items from the api call id, length, dip, name but how do I parse it and save the fields that I need and do they need to be in params? Currently the api call data is not in params.
On button click I want to have those fields listed above save into the db
routes
root 'welcome#index'
post 'search_campaigns', to: 'campaigns#search_all_campaigns'
my model
class Campaign < ApplicationRecord
def self.get_your_campaigns
uri = URI.parse("https://example.site/api/v2/users")
request = Net::HTTP::Get.new(uri)
request.content_type = "application/json"
request.basic_auth("example#email.com", "238urfs393kmdsb2189aead01")
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
return JSON.parse(response.body)
end
end
controller
class CampaignsController < ApplicationController
def search_all_campaigns
#campaigns = Campaign.get_your_campaigns
redirect_to root_path
end
end
view
<%= button_to 'Get All Campaigns', search_campaigns_path %>
This how the api call data looks
[{"id"=>2758, "dip"=>"2.0", "length"=>10, "name"=>"Cereal", "total_remaining"=>100, "status"=>6, "is_retarget"=>false}, {"id"=>278563, "dip"=>"1.25", "length"=>2, "name"=>"Pizza", "total_remaining"=>123, "status"=>6, "supplier_link"=>"http://www.developingmedia.com/adhoc.php?id=", "incidence"=>50, , "days_in_field"=>5, "max_daily_completes"=>nil, "is_retarget"=>false}, {"id"=>278564, "dip"=>"4.25", "length"=>25, "name"=>"California", "days_in_field"=>5,}]
You say the API and therefore you Campaign.get_your_campaigns method returns a Hashthat looks like this:
[
{
"id" => 2758,
"dip" => "2.0",
"length" => 10,
"name" => "Cereal",
"total_remaining" => 100,
"status" => 6,
"is_retarget" => false
},
{
"id" => 278563,
"dip" => "1.25",
"length" => 2,
"name" => "Pizza",
"total_remaining" => 123,
"status" => 6,
"supplier_link" => "http://www.developingmedia.com/adhoc.php?id=",
"incidence" => 50, ,
"days_in_field" => 5,
"max_daily_completes" => nil,
"is_retarget" => false
},
{
"id" => 278564,
"dip" => "4.25",
"length" => 25,
"name" => "California",
"days_in_field" => 5,
}
]
You can use Hash#slice to extract only the attributes you are interested in. And then pass those attributes one after the other to the create method:
campaigns_hashes = Campaign.get_your_campaigns
campaigns_attributes = campaigns_hashes.map { |hash| hash.slice(:id, :name, :length, :dip) }
campaigns = campaigns_attributes.each { |attributes| Campaign.create(attributes) }
Note: You will very likely need to add some error handling to this, for example, to deal with invalid data returned from the API or the handle records that have already been imported to avoid duplicates.
just trying to loop thru my Story model.. and create JSON object and insert it into Call and send it.. But i'm not sure how to loop thru Stories..
I've done this:
#stories = Array.new
Story.where(newsletter_id: current_user.selected_newsletter) do |story|
#stories << {
:title => story.title,
:image_url => story.image_url
}
end
and i'm trying ti insert the loop to this JSON OBJECT
"message" => {
"attachment" => {
"type" => "template",
"payload" => {
"template_type" => "generic",
"elements" => [{this is the array}]
}
}
}
The array with multiple stories should looks like this:
[
{
"title" => "title....1",
"image" => "image....1"
},
{
"title" => "title....2",
"image" => "image....3"
}
....
]
Try the following:
#stories = Story.where(newsletter_id: current_user.selected_newsletter)
.select(:title, 'image_url as image').as_json(except: :id)
And then:
{
"message" => {
"attachment" => {
"type" => "template",
"payload" => {
"template_type" => "generic",
"elements" => #stories
}
}
}
}
I use jqgrid in my rails to show data in my web
and I want to decide which data to show depends on which page the user is surfing
here's the code
show.html.erb
<%=raw jqgrid("people_list", "people", "/people",
[ { :field => "pid", :label => "pid" ,:editable => true},
{ :field => "name", :label => "name" ,:editable => true},
{ :field => "gender", :label => "gender" ,:editable => true},
{ :field => "birthday", :label => "birthday" ,:editable => true}
] ,
{:autowidth=>true,:add => true,:edit => true,:delete => true,:view => true,:edit_url => post_data_people_url,:sortable_rows => 'true'
}
)%>
people_controller.rb
def index
index_columns ||= [:pid,:name,:gender,:birthday,:school]
current_page = params[:page] ? params[:page].to_i : 1
rows_per_page = params[:rows] ? params[:rows].to_i : 10
conditions={:page => current_page, :per_page => rows_per_page}
conditions[:order] = params["sidx"] + " " + params["sord"] unless (params[:sidx].blank? || params[:sord].blank?)
if params[:_search] == "true"
conditions[:conditions]=filter_by_conditions(index_columns)
end
#temp = Person.limit(0).all
KlassesPeople.where(:klass_id => 1).each do |stu|########
#temp.concat( Person.where(:id => stu.person_id) )
end
#people = #temp.paginate(conditions)
total_entries=#people.total_entries
respond_with(#people) do |format|
format.html
format.json { render :json => #people.to_jqgrid_json(index_columns, current_page, rows_per_page, total_entries)}
end
end
I want to pass the value of klass id from the jqgrid to the controller to determine the value of klass_id in the line with "########"
any idea?
I don't know how to format the code for you but you can use the postData parameter to pass a value to your controler every time the jqGrid would get data.
postData: { KeyName: KeyValue },
If you need to set this value dynamically you can set it via something like:
$('#gridName').jqGrid('setGridParam', { postData: { KeyName: KeyValue });
In my rails application, I have a button which when clicked, copies data from one database and insert it in another.
I am using octopus gem to link my application to 2 databases.
To copy a record from db_A to db_B, I am using the code below:
Octopus.using(:shard_B) do
#book_new_live = Book.create(
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_SOURCE => "",
:BK_PUB => "",
:BK_COVER => "",
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_FINAL_LABEL => "",
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_DB => "",
:BK_COVERED_REGION => "",
:BK_VERSION_NO => #book.BK_VERSION_NO,
:BK_SEQ_FILE => "",
)do |primary|
primary.BK_ID = #book.BK_ID
end
end
Database 'db_b', to which data in copied, does not accept null values and the columns cannot be null and the default value is 'NONE'.
Also, I am not allowed to modify the structure of the database so that it can accept null values.
If I use the simplified code below, I get an error message which informs me that the columns 'BK_SOURCE', 'BK_PUB', 'BK_COVER'...cannot be null. By default rails is passing null to those columns.
So I have to pass empty strings to the columns which cannot be null.
Octopus.using(:shard_B) do
#book_new_live = Book.create(
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_VERSION_NO => #book.BK_VERSION_NO,
)do |primary|
primary.BK_ID = #book.BK_ID
end
end
Is there a way of preventing rails from passing null values to the columns not mentioned in the above code?
If i understand you correctly, you can just use the or operator to send your values like this:
Octopus.using(:shard_B) do
#book_new_live = Book.create(
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_SOURCE => #book.BK_SOURCE || "NONE",
:BK_PUB => #book.BK_PUB || "NONE",
:BK_COVER => #book.BK_COVER || "NONE",
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_FINAL_LABEL => "",
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_DB => "",
:BK_COVERED_REGION => "",
:BK_VERSION_NO => #book.BK_VERSION_NO,
:BK_SEQ_FILE => "",
)do |primary|
primary.BK_ID = #book.BK_ID
end
end
By saying that the value to be passed is #book.BK_SOURCE || "NONE", if the attribute is nil, then the string NONE is passed instead.
EDIT
hash = {
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_SOURCE => #book.BK_SOURCE,
:BK_PUB => #book.BK_PUB,
:BK_COVER => #book.BK_COVER,
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_FINAL_LABEL => #book.BK_FINAL_LABEL,
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_DB => #book.BK_DB,
:BK_VERSION_NO => #book.BK_VERSION_NO
}
hash = hash.delete_if { |k, v| v.nil? }
Octopus.using(:shard_B) do
#book_new_live = Book.create(hash)do |primary|
primary.BK_ID = #book.BK_ID
end
end
Give it a try.
For those of you using Rails as a backend to their Sproutcore clients,
which one is the best way to format the data into json?
From the Sproutcore guides there was this approach:
def as_json(options = {})
event_hash = {
"guid" => self.id,
"id" => self.id,
"designation" => self.designation,
"category" => self.category,
"scheduled_for" => self.scheduled_for,
"location" => self.location,
"groups" => self.groups,
"resources" => self.resources
}
event_hash
end
But it fails, send an "Illegal statement error". Then, I changed to this other method:
def as_json(options = {})
# event_hash = options.merge(:include => [:groups, :resources], :methods => :guid)
event_hash = options.merge(:methods => :guid)
super(event_hash)
end
which seems to be working as far as the formatting is concerned, although I am suspecting it to causing some trouble regarding the representation in the dataHash of the store. Anyway, ha anyone been having similar issues with the first version of as_json? If not, is there anything I am doing wrong?
Appreciate any help
On the first method you need to call super:
def as_json(options = {})
event_hash = {
"guid" => self.id,
"id" => self.id,
"designation" => self.designation,
"category" => self.category,
"scheduled_for" => self.scheduled_for,
"location" => self.location,
"groups" => self.groups,
"resources" => self.resources
}
super(event_hash)
end
However you should get the options param and process to do this apropiately.