I'm using find_by_sql with Activerecord which I generate another field there that doesn't in the original table as a combination of different fields like:
select (field1 + field2) as new_field_name
If I try to access the newly generated field like:
@user.new_field_name
I get nothing! How do you suggest I should approach this problem
My situation is like this.
Company has many users and users may belongs to many companies.
And current implementation is something like below.
class Company
has_many :employments
has_many :users, :through = :employments
end
class Employment
belongs_to :company
belongs_to :user
end
class User
has_many :employments
has_many :companies, :through = :employments #This doesn't looks correct
end
User has many companies doesn't looks logically meaningful.It must be some thing like belongs_to_many companies.
Do I need to use has_and_belongs_to_many? But that also will gives the same meaning.
Can some one please suggest the right way for representing these relationships?
I would like to be able to gather all records in a table where the user_id is not null.
This is what I have but it doesn't seem to be working (even though I've had it working in a seperate project):
named_scope :all_registered, :conditions => ["user_id != ?", nil]
This is my code:
[email protected] do |a|
-if @i%3 == 0
%ul
%li=link_to a.name, a
-@i += 1
I need the li to be inside the ul which is inside the if-statement.
I can't do it because of the indentation. Can't I just tell the li to indent automatically?
Thanks
I have a string. That string is a html code and it serves as a teaser for the blog posts I am creating. The whole html code (teaser) is stored in a field in the database.
My goal: I'd like to make that when a user (facebook like social button) likes certain blog post, right data is displayed on his news feeds. In order to do that I need to extract from the teaser in the first occurrence of an image an image path inside src="i-m-a-g-e--p-a-t-h". I succeeded when a user puts only one image in teaser, but if he accidentally puts two images or more the whole thing craches.
Furthermore, for description field I need to extract text inside the first occurrence inside <p> tag. The problem is also that a user can put an image inside the first tag.
I would very much appreciate if an expert could help me resolve this what's been bugging me for days.
Text string with a regular expression for extracting src can be found here: http://rubular.com/r/gajzivoBSf
Thanks!
I've been stuck on something for the past day. I'm building a RoR bot, and part of it involves signing up for an email account with mail.com. I've automated all the filling out of the form, apart from the captcha (Recaptcha).
I'll be using the deathbycaptcha Ruby gem. However, in order to have the captcha solved, I'll need to get either its ID or its URL. While this shows up when I "enable form details" with the Firefox Web Developer toolbar, it doesn't seem to be in the source. How can I find it out? I'm using Watir.
Thanks!
Joe
example: Person has_many Comments. Comments has named_scope :approved. And I need to serialize Person with all approved comments to JSON. So it should look like this:
person.to_json(:include => :comments)
but how can I add there that named scope? To serialize just that approved ones?
does anyone know if it is possible to do a double nested form for. so that i could upload images to a set from an article form.
e.g.
Article
has_many :image_sets
ImageSet
belongs_to :article
has_many :images
Image
belongs_to :image_set
What is the best way to set default value in ActiveRecord?
I see a post from Pratik that describes an ugly, complicated chunk of code: http://m.onkey.org/2007/7/24/how-to-set-default-values-in-your-model
class Item < ActiveRecord::Base
def initialize_with_defaults(attrs = nil, &block)
initialize_without_defaults(attrs) do
setter = lambda { |key, value| self.send("#{key.to_s}=", value) unless
!attrs.nil? && attrs.keys.map(&:to_s).include?(key.to_s) }
setter.call('scheduler_type', 'hotseat')
yield self if block_given?
end
end
alias_method_chain :initialize, :defaults
end
YUCK!
I have seen the following examples googling around:
def initialize
super
self.status = ACTIVE unless self.status
end
and
def after_initialize
return unless new_record?
self.status = ACTIVE
end
I've also seen people put it in their migration, but I'd rather see it defined in the model code.
What's the best way to set default value for fields in ActiveRecord model?
Im trying to get a controller to create a new account with the user id filled in, this doesn't work in my controller or in the console, why? Any suggestions on how to implement this would be much appreciated.
class PaymentNotification < ActiveRecord::Base
after_create :add_account_to_market
private
def add_account_to_market
if status == "Completed"
line = LineItem.find(:first, :conditions => { :cart_id => cart.id })
line.quantity.times do
Account.new(:user_id => cart.user_id)
end
end
end
end
I use Ultrasphinx gem plugin as a wrapper for accessing Sphinx search daemon.
My model declaration:
class Foo < ActiveRecord::Base
is_indexed :fields => ['content', 'private_notes', 'user_id']
Client code:
filters = {}
if type == "private"
# search only in `content` column
filters['user_id'] = current_user.id
else
# search in `content` and `private_notes` columns
end
results = Ultrasphinx::Search.new(:query => params[:query],
:per_page => 20,
:page => params[:page] || 1,
:filters => filters)
The problem I have now with Ultrasphinx gem(or Sphinx, in general?) is that it does not allow me to change set of fields where to look for matches IN RUNTIME
How can I solve this problem?
When I try to pull down a database while at work I get the following error:
:>heroku db:pull
Auto-detected local database: sqlite://db/development.sqlite3
Bad credentials given for http://heroku:[hidden]@taps.heroku.com
Note that when I am at home I am able to run this command just fine. I wanted to know if you have any suggestions for common reasons I would get this error.
Hi,
could you tell me please - how to use method create_translation_table! of globalize2 with additional options such as :null = false, :default = "abc" ???
I think I'm just missing something obvious. I send a user a perishable token embedded in a link. They click on it, and they come back to the site. I want to log them in automatically (I'm not building a banking app).
This seems like this should be simple, but all the examples I've found require a password. How do I skip this completely? I just seem to get UserSession.create to work.
Hi all!
I need to know the ID of the current user in a model:
def after_save
desc, points=nil, nil
if answer_index == daily_question.correct_answer_index
desc = I18n.t('daily_question.point_log.description.correct')
points=daily_question.points
else
desc = I18n.t('daily_question.point_log.description.incorrect')
end
current_user.give_points(:description => desc,
:points => points
)
end
But I guess that is not how it is done?
Regards,
Jacob
Right now I have an initializer that does this:
ActiveRecord::Base.send :has_many, :notes, :as => :notable
ActiveRecord::Base.send :accepts_nested_attributes_for, :notes
It builds the association just fine, except when I load a view that uses it, the second load gives me:
can't dup NilClass
from:
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2184:in `dup'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2184:in `scoped_methods'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2188:in `current_scoped_methods'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2171:in `scoped?'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2439:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2439:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:162:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:162:in `build_association'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:423:in `build_record'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:102:in `build'
(my app)/controllers/manifests_controller.rb:21:in `show'
Any ideas? Am I doing this the wrong way? Interestingly if I move the association onto just the model I'm working with at the moment, I don't get this error. I figure I must be building the global association incorrectly.
Need a simple a way of rounding off an Image. I need the corners to be transparent. This link shows how to do it via command line:
http://www.imagemagick.org/Usage/thumbnails/#rounded
What I need is the corresponding RMagick\Ruby code... Thanks!
I'm using nested routes and I want to provide some sort of a shortcut method. (I'm using RoR 3.0)
The routes look like this.
resources :countries do
resources :regions do
resources :wineries
end
end
To access a winery route I want to be able to define a function that removes the need to specify a country and region each time. Like:
def winery_path(winery)
country_region_winery_path (winery.country, winery.region, winery)
end
Where should I do this? How can I get that to be available whereever url_for is available?
My path helpers (like data_path, users_path) are now translating itself to use '.' instead of '/'.
For example, data_path(@data) translates to:
data.%23<datareport:0x7fcaba0c2970>
instead of '/data/id'.
My routes look like:
map.resources :data
I need to use function "image_path" in my lib class. I tried this (and couple of other variations):
class CustomHelpers::Base
include ActionView::Helpers::AssetTagHelper
def self.image_url(source)
abs_path = image_path(source)
unless abs_path =~ /^http/
abs_path = "#{request.protocol}#{request.host_with_port}#{abs_path}"
end
abs_path
end
end
But it didn't work. Am I doing it right?
Another question is, how do I find the right class to include? For example if I look at this module: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html is there a rule of thumb how to include that module in a model / library / class / anything else ?
Hi, Here is my scenario:
I have three models Subscriber, Subscription, Plan, with has_many :through relationship
between Subscriber and Plans.
A subscriber can have multiple plans with one active plan. Whenever a subscriber selects a plan I save it using accepts_nested_attributes_for :subscriptions. I get one plan from the form.
Now my problem is I want to get the ID of the record created in subscriptions table.